「Ruby入門」Rubyで複数の配列を一つにまとめる(Array#join)

rubyコード:
data = [’1′, ‘2′, ‘3′]
s = ‘ ‘
data.each { |x| s << x << ‘ and a ‘ }
s # => “1 and a 2 and a 3 and a”
data.join(’ and a ‘) # => “1 and a 2 and a 3″
s = “”
data.each_with_index { |x,i| s << x; s << ” and a ” if i < data.length – 1 }
s # => “1 and a 2 and a 3″

Development

Posted by arkgame