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.
配列があるとしましょう。配列を関数に渡したいのですが。ただし、この関数は2つの引数を想定しています。配列を2つの引数にオンザフライで変換する方法はありますか?例えば:
a = [0,1,2,3,4] b = [2,3] a.slice(b)
Rubyでエラーが発生します。 ありがとうa.slice(b[0],b[1])のように、もっとエレガントなものを探していると入力する必要があります。a.slice(foo.bar(b))
a.slice(b[0],b[1])
a.slice(foo.bar(b))
(または「スプラット」)演算子Arrayを使用して、を引数リストに変換できます。*
Array
*
a = [0, 1, 2, 3, 4] # => [0, 1, 2, 3, 4] b = [2, 3] # => [2, 3] a.slice(*b) # => [2, 3, 4]
これを使って
a.slice(*b)
スプラット演算子と呼ばれます