W
w wg
Hi,
I found Array's each method (block) changes its elements sometimes,
but sometimes it don't.
Please look this:
a = [1,2,3,4,5]
b = a.each { |x| x + 1}
puts a
[1,2,3,4,5]
puts b
[1,2,3,4,5]
The array keeps no changed.
But, look another:
a = ["how","are","you"]
b = a.each { |x| x << "-" }
puts a
["how-","are-","you-"]
puts b
["how-","are-","you-"]
The array changed!
I want to know how does Array's each method (block) change its elements ?
Thank you.
I found Array's each method (block) changes its elements sometimes,
but sometimes it don't.
Please look this:
a = [1,2,3,4,5]
b = a.each { |x| x + 1}
puts a
[1,2,3,4,5]
puts b
[1,2,3,4,5]
The array keeps no changed.
But, look another:
a = ["how","are","you"]
b = a.each { |x| x << "-" }
puts a
["how-","are-","you-"]
puts b
["how-","are-","you-"]
The array changed!
I want to know how does Array's each method (block) change its elements ?
Thank you.