为什么这个map表达式会根据我使用大括号还是do/end产生不同的结果?a=[1,2,3,4,5]pa.map{|n|n*2}#=>[2,4,6,8,10]pa.mapdo|n|n*2end#=>[1,2,3,4,5] 最佳答案 那是因为第二行被解释为:p(a.map)do...end代替:p(a.mapdo...end)在这种情况下,语法是不明确的,do似乎没有{那样强。 关于Ruby做/结束vs大括号,我们在StackOverflow上找到一个类似的问题:
选择有意义。但是有人可以向我解释.detect吗?我不明白这些数据。>>[1,2,3,4,5,6,7].detect{|x|x.between?(3,4)}=>3>>[1,2,3,4,5,6,7].detect{|x|x.between?(3,6)}=>3>>[1,2,3,4,5,6,7].detect{|x|x.between?(3,7)}=>3>>[1,2,3,4,5,6,7].detect{|x|x.between?(2,7)}=>2>>[1,2,3,4,5,6,7].detect{|x|x.between?(1,7)}=>1>>[1,2,3,4,5,6,7].detect{|x