Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Ruby 中,我有类似于以下的代码
foo { |x, y| puts y }
因为编译器/解释器不断警告我未使用的 var X,所以我将 x 替换为 '*' 并且编译器停止抱怨。(我不知道为什么我决定 * 是最好的选择......它只是发生了......)
foo { |*, y| puts y }
这到底是做什么的?并且有没有副作用?
此上下文中的星号称为“splat”运算符。这意味着您可以在其位置传递多个参数,并且该块会将它们视为一个数组。
不过,我不确定它是如何或为什么在没有变量名的情况下工作的(例如foo { |*x, y| puts y })。我猜这意味着该块忽略了除最后一个参数之外的所有参数,它会打印出来。
foo { |*x, y| puts y }