10
1
章
Ruby
ひとめぐり
end
=> nil
>>
join_with_commas('Testing: ', 'one', 'two', 'three', '.')
=> "Testing: one, two, three."
*
演算子は、メッセージを送るときに配列の各要素を個別の引数として扱う場合にも使われます。
>>
arguments = ['Testing: ', 'one', 'two', 'three', '.']
=> ["Testing: ", "one", "two", "three", "."]
>>
join_with_commas(
*
arguments)
=> "Testing: one, two, three."
*
は多重代入にも使われます。
>>
before,
*
words, after = ['Testing: ', 'one', 'two', 'three', '.']
=> ["Testing: ", "one", "two", "three", "."]
>>
before
=> "Testing: "
>>
words
=> ["one", "two", "three"]
>>
after
=> "."
1.6.6
ブロック
ブロック(block)は
do/end
もしくは中括弧で囲まれたRubyコードです。メソッドは暗黙のブロッ
ク引数を取ることができ、
yield
キーワードによって、そのブロック内のコードを呼び出せます。
>>
def do_three_times
yield ...