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
分享到:
相关推荐
Programming Ruby - The Pragmatic Programmer's Guide, 2nd Edition (2005) [annotated]
Programming-Ruby-1.9一书中的源代码
Programming Ruby中文版第二版[高清扫描版][带书签]。 Programming.Ruby-2nd[高清文字版][带
Programming ruby ruby编程入门书籍,简要讲解ruby的下载,安装,以及简单的使用。
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 英文的,不过觉得读起来不吃力,习惯就好了,程序员如果不习惯读英文,会错过非常多的好书
内含以下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 第二版(Second Edition)英文版于2005年出版,之后有了中文版。但可惜的是网上流传的中文版PDF基本上都是扫描版的,文字内容得自己打出来,看到好的代码也得自己动手,无法Ctrl-C + Ctrl-V,还...
Programming Ruby English version and the source Code
Ruby-Functional-Programming, 来自 Conferencia Rails 2011 通过 Arnau Sanchez实现的ruby 函數式程式設計簡介理論部分Ruby的函數式程式設計不要更新變數不要重用變量用阻止作為高階函數物件導向與函數式程式設計萬...