Bug #33 [ruby-reference-manual:808]
Enumerable#injectの説明
| Status : | Closed | Start : | 02/13/2008 | |
| Priority : | Normal | Due date : | ||
| Assigned to : | - | % Done : | 100% |
|
| Category : | doc | |||
| Target version : | - | |||
| reporter : | rock |
ruby_version : | 1.8 |
|
Description
Enumerable#inject の説明の「合計を計算する」の例が
p [1, 2,3, 4, 5].inject(0) {|result, item| result + item }
となっていますが,無駄な計算をさせないため inject の初期値を省略すべきと思います。
逆に初期値の必要な例として,
p %w(u b i).inject("text"){|result, tag| "<#{tag}>#{result}</#{tag}>"}
みたいのを挙げるのはいかがでしょうか。
Associated revisions
Fixed #33