do/end 和{}的优先级问题

do/end 和{}的优先级问题

看Programming php?name=Ruby" onclick="tagshow(event)" class="t_tag">Ruby2第188页的例子程序:

def one(arg)
if block_given?
  "block given to 'one' returns #{yield}"
else
 arg
end
end

def two
 if block_given?
  "block given to 'two' returns #{yield}"
 end
end

result1 = one two {
"three"
}
result2 = one two do
"three"
end
puts "With braces, result = #{result1}"
puts "With do/end, result = #{result2}"
produces:
With braces, result = block given to 'two' returns three
With do/end, result = block given to 'one' returns three

对于do/end的结果,我看不懂,是怎么执行得来的?
从例子看来
如果用braces,是临近原则,传给离{}最近的那个方法。
do。。。end则不是
改一下。。。

[Copy to clipboard] [ - ]