Ruby语言比较讲座:方法和过程块

『》』什么是块过程(proc/procs)?
『《』由do和end(或{和})组成的代码,称之为块过程。

『》』Ruby语言的方法(method/methods)和过程块(proc/procs)的区别
『《』下面我们说明方法和过程块的区别。在Ruby语言中,我们不能把方法名作为参数传递给另一个方法(但是我们可以把过程块名作为参数传递给方法),并且方法不能够返回其它方法(但是它们可以返回过程块)。之所以如此简单。是因为过程块都是对象,而方法不是。

In particular, you can't pass methods into other methods (but you can pass procs into methods), and methods can't return other methods (but they can return procs). This is simply because procs are objects; methods aren't.

下面是实例代码:
def doSelfImportantly someProc
puts 'Everybody just HOLD ON! I have something to do...'
someProc.call
puts 'Ok everyone, I\'m done. Go on with what you were doing.'
someProc
end

sayHello = Proc.new do
puts 'hello'
end

myproc = doSelfImportantly sayHello
puts '----------------------------'
myproc.call

『》』什么是块(block/blocks)?
『《』在do和end(或{和})之间的代码,称之为块。

Comments

Popular posts from this blog

英语字母a的发音