(Ruby1.9.3)
seq = ["foo", "bar"] p seq.inject(0) { |sum, str| sum + str.length } # >> 6 p seq.map(&:length) # >> [3, 3] p seq.inject(:+) # >> "foobar" p seq.map(&:length).inject(:+) # >> 6
牌語備忘録 -pygo
(Ruby1.9.3)
seq = ["foo", "bar"] p seq.inject(0) { |sum, str| sum + str.length } # >> 6 p seq.map(&:length) # >> [3, 3] p seq.inject(:+) # >> "foobar" p seq.map(&:length).inject(:+) # >> 6