Array#join

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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″