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.
分享到:
相关推荐
詹姆斯·布里特,ruby-doc.org网站管理员,表示第一版的《Programming Ruby》就是一本智慧、优雅且充满趣味的书,而第二版更是超越前者,成为Ruby学习者的首选资料。 ### 学习Ruby的意义 查德·福勒(Chad Fowler...
《Programming Ruby 1.9》是一本经典的Ruby编程语言教程,其源代码包含了大量实例和示例,旨在帮助读者深入理解Ruby的语法、特性以及编程实践。这些源代码是学习和探索Ruby语言的重要资源,涵盖了从基础语法到高级...
3. 名人推荐:书中引用了多位业界领袖和专家的观点,如Martin Fowler、Mike Clark、James Britt、Chad Fowler、Ryan Davis和Rich Kilmer等,他们均表示这本书对学习Ruby语言以及Ruby编程思维产生了深远的影响。...
《Programming Ruby 1.9》是由Dave Thomas、Chad Fowler以及Andy Hunt三位作者共同编写的关于Ruby编程语言的一本权威指南。这本书是对之前版本《Programming Ruby》的大幅修订版,并得到了原出版社Addison Wesley的...
3. **Ruby-2.5系列的特性**: - **性能提升**:Ruby 2.5引入了JIT(Just-In-Time)编译器,提升了执行效率。 - **线程改进**:多线程性能得到了优化,减少了全局解释器锁(GIL)对并发的影响。 - **语法糖**:...
3. **块、迭代器与闭包**:Ruby中的块是代码的可执行部分,可以与方法一起使用,形成迭代器。闭包是能访问其自身作用域的函数,这在函数式编程中非常关键。书中会深入解释这些特性及其应用。 4. **元编程**:Ruby的...
"Ruby语言入门教程附实例"和"ruby-mht"文件很可能是包含实例的教程,实践是学习编程的关键,通过这些实例,你可以亲手操作,从而巩固理论知识。 总的来说,这个压缩包提供了全面的Ruby学习资源,既有理论讲解,也有...
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 ...
《Programming Ruby》是一本关于Ruby编程语言的经典著作,由Dave Thomas、Andy Hunt和Chad Fowler合著。这本书自2004年初版以来,一直是学习Ruby的首选资源,被誉为“Pickaxe”书,因其封面的图标而得名。Ruby是一种...
3. **函数式编程**:虽然Ruby主要被视为面向对象的语言,但它也支持函数式编程风格,如使用map、reduce等函数进行数据处理。 4. **多重继承**:Ruby支持多重继承,一个类可以从多个父类继承属性和方法,从而增强了...
Ruby - Ruby 开发 - 常用知识点 backtracking、bit_manipulation、ciphers、conversions、data_structures、discrete_mathematics、dynamic_programming、electronics、maths
3. **Ruby的数据结构**: - 数组(Array):线性数据结构,支持索引访问和各种操作,如插入、删除、合并等。 - 哈希(Hash):键值对存储,提供了快速查找功能,键可以是任何对象。 - 符号(Symbol):不可变的...
3. **动态性**:Ruby的动态性体现在运行时可以改变类、方法和变量的定义,允许动态绑定和鸭子类型。这为开发过程提供了极大的灵活性,但同时也要求开发者具备更高的责任感。 4. **块、 Proc 和 Lambda**:Ruby中的...
3. **块、Proc和Lambda**:Ruby中的代码块是一种特殊的语法结构,可以与 Proc 和 Lambda 对象关联,用于创建可重用的代码片段。 4. **模块(Module)**:Ruby的模块用于实现命名空间、混合(Mix-in)功能,以及提供...
内含以下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》是一本经典的Ruby编程语言教程,中文第二版为中国的程序员提供了深入学习Ruby的宝贵资源。这本书的源代码下载对于读者来说是极其有用的,因为它允许读者在实践中探索和理解书中所阐述的概念。...