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.
次のような方法がある場合:
def sum *numbers numbers.inject{|sum, number| sum += number} end
配列を数値として渡すにはどうすればよいですか?
ruby-1.9.2-p180 :044 > sum 1,2,3 #=> 6 ruby-1.9.2-p180 :045 > sum([1,2,3]) #=> [1, 2, 3]
配列を受け入れるように sum メソッドを変更できないことに注意してください。
メソッドを呼び出すときにスプラットを置くだけですか?
sum(*[1,2,3])
これのことですか?
@Dogbertが最初でした