Array#join

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″
我们自己也可以模拟Array#join的效果:
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″ 

编程技巧