我提取了一个简单的例子:
require 'pp'
x = 1..3
pp x.map do |i| {:value => i, :double => (i*2)} end
pp x.map { |i| {:value => i, :double => (i*2)} }
pp(x.map do |i| {:value => i, :double => (i*2)} end)
pp(x.map { |i| {:value => i, :double => (i*2)} })
我想知道为什么第一个pp会产生以下输出:[1, 2, 3]
虽然所有订单都在提供:
[{:value=>1, :double=>2}, {:value=>2, :double=>4}, {:value=>3, :double=>6}]
我认为这与操作符优先级有关。在哪里可以找到好的解释?
each
works with brackets but not withdo
-end
(ruby)、Block definition - difference between braces anddo
-end
?、Ruby multiline block withoutdo
end
、Usingdo
block vs brackets{}
和 What is the difference or value of these block coding styles in Ruby? 的副本。 - Jörg W Mittag