Edited at

Enumerable#inject のブロック内の制御にちょっと注意!

More than 5 years have passed since last update.

injectで割と以下の失敗をよくします。。。



pry

2.0.0 (main)> (0..2).inject([]) {|ary, i| next if i == 1; ary << i}


NoMethodError: undefined method `<<' for nil:NilClass

from (pry):2:in `block in __pry__'



pry

2.0.0 (main)> (0..2).inject([]) {|ary, i| next ary if i == 1; ary << i}


=> [

[0] 0,
[1] 2
]