Array#join

2015-05-28 0 915
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″ 

遇见资源网 ruby Array#join http://www.ox520.com/16363.html

下一篇: 距离换算器
常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务