8
我想了解*(1..9)
和[*1..9]
了解红宝石图示
之间的区别,如果我把它们分配给变量,他们的工作方式相同
splat1 = *(1..9) # splat1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
splat2 = [*1..9] # splat2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
但事情变得奇怪,当我尝试直接使用*(1..9)
和[*1..9]
。
*(1..9).map{|a| a.to_s} # syntax error, unexpected '\n', expecting tCOLON2 or '[' or '.'
[*1..9].map{|a| a.to_s} # ["1", "2", "3"...]
我猜是问题的一部分是与运营商precidence?但我不确定发生了什么事。为什么我无法使用*(1..9)
我可以使用[*1..9]
?
'P *(1..9){.MAP | A | a.to_s}'工作正常 – Bohdan
同样,'([* 1..9])map'应该做工精细。不过,我认为明确的'to_a'方法更加清晰,基本上同样简洁。 – maerics