Ruby块和不带括号的参数我提取了简单的例子:require 'pp'x = 1..3pp x.map do |i| {:value => i, :double => (i*2)} endpp 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}]我认为它与运算符优先级有关。我在哪里可以找到好的解释?
2 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
在此帖子中使用Ruby-2.2.0的pry会话的部分日期。pp_hash = x.map do | i | {:value => i,:double =>(i * 2)} end => [{:value => 1,:double => 2},{:value => 2,:double => 4},{ :value => 3,:double => 6}] pp_hash.join(“,”)“{:value => 1,:double => 2},{:value => 2,:double => 4}, {:value => 3,:double => 6}“这在今天的Json用法中可能更为理想,但这只是对输出读取的风格问题。有时,只要您的代码可以处理正确的类型,您将要传递哪一个并不重要。pp_hash = pp_hash.join(“,”)
- 2 回答
- 0 关注
- 609 浏览
添加回答
举报
0/150
提交
取消