LoginSignup
0
0

More than 5 years have passed since last update.

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

Last updated at Posted at 2013-11-15

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
]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0