1, about closure
If the last parameter in a method definition is prefixed with an ampersand (such as &action),
Ruby looks for a code block whenever that method is called. That code block is converted
to an object of class Proc and assigned to the parameter. You can then treat
the parameter as any other variable. In our example, we assigned it to the instance
variable @action. When the callback method button_pressed is invoked, we use the
Proc#call method on that object to invoke the block.
So what exactly do we have when we create a Proc object? The interesting thing is that
it’s more than just a chunk of code. Associated with a block (and hence a Proc object)
is all the context in which the block was defined: the value of self and the methods,
variables, and constants in scope. Part of the magic of Ruby is that the block can still
use all this original scope information even if the environment in which it was defined
would otherwise have disappeared. In other languages, this facility is called a closure.
2, Regular expressions
match operators =~ (positive match) and !~ (negative match).
$& receives the part of the string that was matched by the pattern, $` receives the part of the string that preceded the match, and $' receives the string after the match.
The match also sets the thread-global variables $~ and $1 through $9. The variable
$~ is a MatchData object (described beginning on page 516) that holds everything you
may want to know about the match. $1, and so on, hold the values of parts of the match.
Within a pattern, all characters except ., |, (, ), [, ], {, }, +, \, ^, $, *, and ? match themselves. If you want to match one of these special characters literally, precede it with a backslash.
A backslash followed by an alphanumeric character is used to introduce a special match construct.
The patterns ^ and $ match the beginning and end of a line, respectively.
The sequence \A matches the beginning of a string, and \z and \Z match the end of a string. (Actually, \Z matches the end of a string unless the string ends with a \n, it which case it matches just before the \n.)
Similarly, the patterns \b and \B match word boundaries and nonword boundaries, respectively.
[characters] matches any single character between the brackets.
Within the brackets, the sequence c1-c2 represents all the characters between c1 and c2, inclusive.
Finally, a period ( . ) appearing outside brackets represents any character except a newline
... todo
分享到:
相关推荐
詹姆斯·布里特,ruby-doc.org网站管理员,表示第一版的《Programming Ruby》就是一本智慧、优雅且充满趣味的书,而第二版更是超越前者,成为Ruby学习者的首选资料。 ### 学习Ruby的意义 查德·福勒(Chad Fowler...
2. Ruby语言的特点:Ruby被描述为一个强大且实用的编程语言,它聪明、优雅并充满乐趣。使用Ruby可以改变程序员的编程思维,因为它提供了一种不同于其他语言的思考方式。 3. 名人推荐:书中引用了多位业界领袖和专家...
《Programming Ruby 1.9》是一本经典的Ruby编程语言教程,其源代码包含了大量实例和示例,旨在帮助读者深入理解Ruby的语法、特性以及编程实践。这些源代码是学习和探索Ruby语言的重要资源,涵盖了从基础语法到高级...
2. **面向对象编程**:Ruby是彻头彻尾的面向对象语言,每个值都是一个对象,包括基本类型。书中会讲解类、对象、继承、模块、多态性等概念,以及如何使用Ruby进行面向对象设计。 3. **块、迭代器与闭包**:Ruby中的...
《Programming Ruby 1.9》是由Dave Thomas、Chad Fowler以及Andy Hunt三位作者共同编写的关于Ruby编程语言的一本权威指南。这本书是对之前版本《Programming Ruby》的大幅修订版,并得到了原出版社Addison Wesley的...
- 解压`ruby-2.5.8.tar.gz`:使用`tar -zxvf ruby-2.5.8.tar.gz`命令解压。 - 编译与安装:进入解压后的目录,运行`./configure`,然后`make`和`make install`进行编译和安装。 - 验证安装:通过`ruby -v`检查...
"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是一种...
2. **元编程**:Ruby允许程序在运行时修改自身结构或行为,这种能力被称为元编程。通过元编程,可以实现高度灵活和可定制的代码。 3. **函数式编程**:虽然Ruby主要被视为面向对象的语言,但它也支持函数式编程风格...
Ruby - Ruby 开发 - 常用知识点 backtracking、bit_manipulation、ciphers、conversions、data_structures、discrete_mathematics、dynamic_programming、electronics、maths
2. **Ruby的面向对象编程**: - 类与对象:Ruby中的万物皆对象,每个类都是Object类的子类。 - 继承与模块:Ruby支持单继承,但可以通过模块实现多重继承的效果,提供代码重用。 - 方法定义:包括实例方法、类...
《Programming Ruby》是一本经典的Ruby编程语言教程,中文第二版为中国的程序员提供了深入学习Ruby的宝贵资源。这本书的源代码下载对于读者来说是极其有用的,因为它允许读者在实践中探索和理解书中所阐述的概念。...
2. **面向对象编程**:Ruby是完全面向对象的语言,一切皆对象,包括基本类型如整数、字符串和布尔值。类是对象的蓝图,实例是类的具体化。Ruby中的继承、封装和多态等面向对象概念被充分支持。 3. **动态性**:Ruby...