易截截图软件、单文件、免安装、纯绿色、仅160KB

Use lambda in Ruby 九筒一条

http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Understanding Ruby Blocks, Procs and Lambdas
Blocks, Procs and lambdas (referred to as closures
in Computer Science) are one of the most powerful aspects of Ruby, and
also one of the most misunderstood. This is probably because Ruby
handles closures in a rather unique way. Making things more complicated
is that Ruby has four different ways of using closures, each of which is
a tad bit different, and sometimes nonsensical. There are quite a few
sites with some very good information about how closures work within
Ruby. But I have yet to find a good, definitive guide out there.
Hopefully, this tutorial becomes just that.
First Up, Blocks
The most common, easiest and arguably most “Ruby like” way to use
closures in Ruby is with blocks. They have the following familiar
syntax:
array
=
[
1
,
2
,
3
,
4
]

array
.
collect!
do
|
n
|
n
**
2
end
puts
array
.
inspect
# => [1, 4, 9, 16]
So, what is going on here?
First, we send the collect!
method to an Array with a
block of code.
The code block interacts with a variable used within the collect!
method (n
in this case) and squares it.
Each element inside the array is now squared.
Using a block with the collect!
method is pretty easy,
we just have to think that collect!
will use the code
provided within the block on each element in the array. However, what
if we want to make our own collect!
method? What will it
look like? Well, lets build a method called iterate!
and
see.
class
Array
def
iterate!
self
.
each_with_index
do
|
n
,
i
|
self
[
i
]
=
yield
(
n
)
end
end
end
array
=
[
1
,
2
,
3
,
4
]
array
.
iterate!
do
|
n
|
n
**
2
end
puts
array
.
inspect
# => [1, 4, 9, 16]
To start off, we re-opened the Array class and put our iterate!
metho


相关文档:

Ruby on Rails中select使用方法

在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆。常见的有三个..
select, select_tag, collection_select(其余的什么select_date那些不谈)
我们先来看看一个基本的下拉式选项骨架
</p>
<select
name="ROR">
<option
value="1">ROR1</option><br
/>
<optio ......

Ruby watir 测试框架

转自 http://www.advidea.cn/biancheng/200943135232.html 
Ruby watir 测试框架
大多数人都会安装 ruby,
也通过Ruby 安装 gem,
也安装了ruby IDE开发工具:netbeans,
但就是不能跑watir环境,狂晕加吐中。。。
错误如下:
in `require': no such file to load -- watir (LoadError)
反正就是找不到watir,这里 ......

ruby的类与模块(1)

class Point
@x = 1
@y = 2
def initialize(x,y)
@x,@y = x,y
end
end 
代码中的@x,@y为实例变量,实例变量只对self的环境起作用,因此initialize外面的@x=1,@y=2只对类本身起作用,而方法内部,的@x,@y是对对象的实例起作用的。
class Point
include Enumerable
def initialize(x ......

ruby和MinGW的一次融合

我们在使用C编程时会遇到一个问题,比如头文件的一个函数包含在一个lib中,但是
在实际连接中我们不知道它在哪个库中。也许可行的一种办法是直接上网查询某个
函数的依赖条件,这对于常用函数是没问题的!但是对于复杂而又缺少文档的第三方
lib来说,无异于大海捞针。
自此通过2种办法来尝试解决这个问题,我们先看第一 ......

Ruby实践—HelloWorld

开发环境
OS:WindowsXP
Ruby: Ruby1.9.1
Rails: Rails2.3.5
IDE: RubyMine2.0.1
1、创建Rails工程
2、修改 /config/database.yml
自动创建的工程中默认数据库连接的是sqlite,如果没有安装此数据库,需要修改该配置(本例中使用的是mysql)
# Mysql Version 5.1.46
development:
adapter: mysql
database: ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号