Ruby 1.9.2 代码:
def append_string_to_text(string_to_append)
string_to_alter = 'starting_bit'
p "OUTSIDE BLOCK: string_to_alter.object_id is #{string_to_alter.object_id}"
Proc.new do
p "****** START OF BLOCK: string_to_alter.object_id is #{string_to_alter.object_id}"
p "defined?(new_string_created_in_block) is #{defined?(new_string_created_in_block) == true}"
unless defined?(new_string_created_in_block)
p "new_string_created_in_block is undefined. lets define it."
new_string_created_in_block = 'test'
end
p "new_string_created_in_block.object_id is #{new_string_created_in_block.object_id}"
string_to_alter = string_to_alter + string_to_append
p "END OF BLOCK: string_to_alter.object_id is #{string_to_alter.object_id}"
string_to_alter
end
end
proc = append_string_to_text('_text_at_the_end')
p proc.call
p proc.call
输出:
"OUTSIDE BLOCK: string_to_alter.object_id is 70283335840820"
"****** START OF BLOCK: string_to_alter.object_id is 70283335840820"
"defined?(new_string_created_in_block) is false"
"new_string_created_in_block is undefined. lets define it."
"new_string_created_in_block.object_id is 70283335840520"
"END OF BLOCK: string_to_alter.object_id is 70283335840440"
"starting_bit_text_at_the_end"
"****** START OF BLOCK: string_to_alter.object_id is 70283335840440"
"defined?(new_string_created_in_block) is false"
"new_string_created_in_block is undefined. lets define it."
"new_string_created_in_block.object_id is 70283335840180"
"END OF BLOCK: string_to_alter.object_id is 70283335840100"
"starting_bit_text_at_the_end_text_at_the_end"
第一次运行块时,string_to_alter
变量最初指向在append_string_to_text
方法开始时创建的对象,因为块是一个闭包。该块创建一个新变量new_string_created_in_block
,然后创建一个新的块局部变量string_to_alter
,它遮蔽了外部变量string_to_alter
。
第二次运行该块时,string_to_alter
变量最初指向在第一次运行块时创建的对象。
为什么在第二次运行期间new_string_created_in_block
未定义?它在第一次运行时被分配,string_to_alter
变量的分配从第一次运行时就被持久化了,那么为什么不new_string_created_in_block
持久化呢?