JanGaJan.com

Is fun? JOY!

メモ : Array#each_with_objectの簡単な使い方

each_with_objectの簡単な使い方。 使い方は気をつけないといけないですね。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[1,2,3].each_with_object(n=[]){|v| n << v + 5}
=> [6, 7, 8]
# nを初期化しないで処理
[1,2,3].each_with_object(n){|v| n << v + 5}
=> [6, 7, 8, 6, 7, 8]
[1,2,3].each_with_object(n){|v| n << v + 5}
=> [6, 7, 8, 6, 7, 8, 6, 7, 8]
# nをeach_with_objectで初期化
[1,2,3].each_with_object(n=[]){|v| n << v + 5}
=> [6, 7, 8]
# nをeach_with_objectの前に宣言
n = [10]
=> [10]
[1,2,3].each_with_object(n){|v| n << v + 5}
=> [10, 6, 7, 8]

Comments