0
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 3 years have passed since last update.

injectの解釈

Posted at

#eachより簡潔に書ける

inject.rb
def keisan
  (1..20).inject(0) do | sum, num|
    sum += num
  end
end
#sum = 0 inject(0)←かっこ内(引数)が変数の初期値。

ただし、メソッドを書いてそこの値を戻すようにしないと、
メソッドの外(スコープ外のため)では使えない。

eachとの使い分けは「見やすいかどうか」で決める。
Rubyは自由度が高いようので、
色んな書き方ができそう。

他人が見たときにわかりやすく書くことが重要

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?