1, about method
Methods that act as queries are often named with a trailing ?, such as instance_of?.
Methods that are “dangerous,” or modify the receiver, may be named with a trailing !.
And methods that can be assigned to (a feature we discussed on page 29) end with an equals sign (=). ?, !, and = are the only “weird” characters allowed as method name suffixes.
Ruby lets you specify default values for amethod’s arguments—values that will be used if the caller doesn’t pass them explicitly. You do this using the assignment operator.
what if you want to pass in a variable number of arguments or want to capture multiple arguments into a single parameter? Placing an asterisk before the name of the parameter after the “normal” parameters does just that.
However, if the last parameter in a method definition is prefixed with an ampersand, any associated block is converted to a Proc object, and that object is assigned to the parameter.
When you call a method, you can explode an array, so that each of its members is taken as a separate parameter. Do this by prefixing the array argument (which must follow all the regular arguments) with an asterisk.
If the last argument to a method is preceded by an ampersand, Ruby assumes that it is a Proc object. It removes it from the parameter list, converts the Proc object into a block, and associates it with the method.
The first difference is that, unlike Procs, lambdas check the number of arguments passed.
The second difference is that lambdas have diminutive returns. What this
means is that while a Proc return will stop a method and return the
value provided, lambdas will return their value to the method and let
the method continue on.
So, when to use Proc over lambdas and vice versa? Honestly, besides
argument checking, the difference is just in how you see closures. If
you want to stay in the mindset of passing blocks of code, keep with
Proc. If sending a method to another method that can return a method
makes sense to you, use lambdas. But, if lambdas are just methods in
object form, can we store existing methods and pass them just like
Procs? For that, Ruby has the something pretty tricky up its sleeve.
Just as you guessed, square
is not a Proc, but a Method.
The neat thing is that this Method object will act just like a lambda,
because the concept is the same. This method however, is a named method
(called square
) while lambdas are anonymous methods.
So to recap, we went through Ruby’s four closure types, blocks, Procs,
lambdas and Methods. We also know that blocks and Procs act like
drop-in code snippets, while lambdas and Methods act just like methods.
分享到:
相关推荐
Programming Ruby - The Pragmatic Programmer's Guide, 2nd Edition (2005) [annotated]
Programming-Ruby-1.9一书中的源代码
Programming ruby ruby编程入门书籍,简要讲解ruby的下载,安装,以及简单的使用。
Programming Ruby中文版第二版[高清扫描版][带书签]。 Programming.Ruby-2nd[高清文字版][带
Programming_ruby-2nd.pdf 全英文书籍。。。。。。。
With active user groups formed in the world’s major cities and Ruby-related conferences filled to capacity. Ruby-Talk, the primary mailing list for discussion of the Ruby language, climbed to an ...
Ruby学习资料(含参考手册和Programming Ruby)-中文.rar
著名的The Pragmatic Programmer一书作者的又一力作,绝对值得珍藏。
The Ruby Programming Language - Oreilly.pdf
Programming Ruby, Ruby程序设计
Programming Ruby 2.0 1.9 pdf
Programming Ruby 1.9 (3rd edition)和源码
Programming ruby.pdf 英文的,不过觉得读起来不吃力,习惯就好了,程序员如果不习惯读英文,会错过非常多的好书
Programming Ruby 第二版(Second Edition)英文版于2005年出版,之后有了中文版。但可惜的是网上流传的中文版PDF基本上都是扫描版的,文字内容得自己打出来,看到好的代码也得自己动手,无法Ctrl-C + Ctrl-V,还...
内含以下4个文档: 1、Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.chm 2、O'Reilly.Learning.Ruby.May.2007.chm 3、Programming Ruby 2e.pdf 4、ruby中文文档.chm
Programming Ruby English version and the source Code
Ruby-Functional-Programming, 来自 Conferencia Rails 2011 通过 Arnau Sanchez实现的ruby 函數式程式設計簡介理論部分Ruby的函數式程式設計不要更新變數不要重用變量用阻止作為高階函數物件導向與函數式程式設計萬...
PROGRAMMING+RUBY中文版 PROGRAMMING+RUBY中文版 PROGRAMMING+RUBY中文版
Programming Ruby中文版 (第二版).pdf