example 1 works, I tried to avoid being repetitive and I got example 2
which doesn’t work. I am using Ruby 1.9.3
#--------------------------------------------
#example 1
def do_something(x)
x.upcase
.upcase is just for this example, I use far more complicated
function in my code
end
my_array = [“aaa”, “bbb”, “ccc”]
a, b, c = my_array
a = do_something(a)
b = do_something(b)
c = do_something©
puts "a = " + a
puts "b = " + b
puts "c = " + c
#output:
a = AAA
b = BBB
c = CCC
#--------------------------------------------
#example 2
def do_something(x)
x.upcase
end
my_array = [“aaa”, “bbb”, “ccc”]
a, b, c = my_array
[a,b,c].map! { |x| x = do_something(x) }
puts "a = " + a
puts "b = " + b
puts "c = " + c
#output:
a = aaa
b = bbb
c = ccc
#--------------------------------------------
#example 3
def do_something(x)
x.upcase
end
x = [“aaa”, “bbb”, “ccc”]
x.map! { |n| do_something(n) }
puts x.inspect
#---------------------------------------------
Example 3 also works as expected … whats wrong with example 2 ?!?