1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby の enum.inject の初期値

Posted at

ブロックなしの enum.inject では初期値を設定できる。総和を求めるときなどに enum が空の可能性がある場合、大抵は初期値 0 を設定しておくほうが都合がいいだろう。

[1, 2, 3].inject(0, :+)  # => 6
[1, 2, 3].inject(:+)     # => 6
[].inject(0, :+)         # => 0
[].inject(:+)            # => nil
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?