二、Kernel 和 Object
1、引入了BasicObject对象,作为一个顶级的空白状态对象:
<!---->BasicObject.instance_methods
#
=> [:==,:equal?,:"!",:"!=",:__send__]
Object.ancestors
#
=> [Object, Kernel, BasicObject]
引入这个对象对于Ruby对象体系带来的影响我还不清楚。
2、instance_exec方法,允许传递参数、self到一个block并执行之,也就是说为特定的instance执行block。
<!---->def
magic(obj)
def
obj.foo(
&
block)
instance_exec(self, a, b,
&
block)
end
end
o
=
Struct.new(:a,:b).new(
1
,
2
)
magic(o)
puts o.foo{
|
myself,x,y
|
puts myself.inspect;x
+
y }
更多例子:
<!---->o
=
Struct.new(:val).new(
1
)
o.instance_exec(
1
){
|
arg
|
val
+
arg }
=>
2
在Ruby 1.8中实现这个方法:
<!---->class
Object
module InstanceExecHelper; end
include InstanceExecHelper
def
instance_exec(
*
args,
&
block)
#
!> method redefined; discarding old instance_exec
mname
=
"
__instance_exec_#{Thread.current.object_id.abs}_#{object_id.abs}
"
InstanceExecHelper.module_eval{ define_method(mname,
&
block) }
begin
ret
=
send(mname,
*
args)
ensure
InstanceExecHelper.module_eval{ undef_method(mname) } rescue nil
end
ret
end
end
3、Kernel的require方法载入的文件将以完整路径存储在变量$"中,等价于:
<!---->$
"
<< File.expand_path(loaded_file)
通过在irb中观察$"变量即可看出差别。
4、Object#tap方法,将对象传入block并返回自身,用于链式调用:
<!---->"
hello
"
.tap{
|
a
|
a.reverse!}[0]
#
=> "o"
"
F
"
.tap{
|
x
|
x.upcase!}[0]
#
=> "F" (注意到"F".upcase!返回的是nil)
5、Kernel#instance_variable_defined?方法:
<!---->a
=
"
foo
"
a.instance_variable_defined? :@a
#
=> false
a.instance_variable_set(:@a,
1
)
a.instance_variable_defined? :@a
#
=> true
6、Object#=~
匹配失败的时候返回nil而不是false
<!---->1
=~
1
#
=> nil
7、Kernel#define_singleton_method 方法,
<!---->a
=
""
a.define_singleton_method(:foo){
|
x
|
x
+
1
}
a.send(:foo,
2
)
=>
3
a.foo(
2
)
=>
3
8、Kernel#singleton_methods, Kernel#methods,返回的是将是方法名symbol组成的数组,过去是方法名的字符串数组。
分享到:
相关推荐
Programming Ruby 1.9 (3rd edition)和源码
Programming Ruby 2.0 1.9 pdf
NULL 博文链接:https://hlee.iteye.com/blog/599644
You’ll also find that Ruby 2 is faster, and has memory management improvements that make it more server-friendly. All told, there are over 110 sections of the book that have been flagged and cross-...
Programming Ruby 1.9 The Pragmatic Programmers' Guide -- Ruby 编程的圣经教程。此版本涵盖最新的Ruby 1.9
Programming Ruby 1.9 3rd edition.Cover ruby 1.9.2, downloaded from pragmatic bool shelf, please enjoy.
Programming Ruby 1.9
[Pragmatic Bookshelf] Ruby 1.9 & 2.0 编程 (英文版) [Pragmatic Bookshelf] Programming Ruby 1.9 & 2.0 The Pragmatic Programmers' Guide 4th Edition (E-Book) ☆ 出版信息:☆ [作者信息] Dave Thomas , ...
ruby 1.9上的轻量级网络开发框架,提供generator!
一些Ruby程序员将Ruby 1.9的发布看做是迈入Ruby新版本的标志性事件,但他们却发现在1.8版本和1.9版本语言之间的刻意不兼容性会导致原有Ruby代码无法工作。真正的问题是,Ruby 1.9.0发布的原因以及其与Ruby1.8之间的...
2009年的新书--Programming Ruby 1.9
最新的ruby 教程 2009 年 ruby 已经发展到了1.9 2.0要到2010年了
2、ruby 1.9正式支持内核多线程机制,可以大大提高ruby作为服务端程序的并发处理能力 3、ruby 1.9还引入了纤程这样的多任务并发处理机制,提供了轻量级并发处理能力。 等等。ruby 1.9.1 的下载地址是:ftp://ftp....
Programming Ruby 1.9 3rd Edition – FreePdfBook
Ruby的Block块是它的关键特色之一,用块能够写出简明且高度可重用的算法。即使没有别的用处,它至少消弱了人们对循环敬畏的态度。这个概念在其他语言和理论中还被称为:Lambda函数。Lambda是个十分令人迷惑的词汇,...
Ruby 1.9(.2)是一种令人赞叹的语言,可用于开发应用程序。它比Ruby 1.8。×更快,更强大,更干净,并且具有巨大的改进。 由于这些原因,每个Ruby开发人员都应该转向我们的语言的这一激动人心的新版本。 当采取...
把它解压到windows\\system32就可以了! 博文链接:https://ennic.iteye.com/blog/151324
Ruby编程语言_涵盖Ruby 1.8和1.9