finer recliner
September 6th, 2008, 04:59 AM
the following code:
letters = ['a','b','c','d']
letters.each do |x|
x.upcase!
if x == 'B' then
x = 'z'
end
end
puts letters
produces this output:
$ ruby test.rb
A
B
C
D
how come inside the "each" block, the upcase! method worked, but the assignment operator inside the "if" statement did not work?
if it was as simple as "x" is a local variable in the "each" block, then wouldn't both the upcase! method and the assignment operator not stick when you exit the loop?
how can i make the the assignment operator stick even after exiting the loop?
thanks guys.
letters = ['a','b','c','d']
letters.each do |x|
x.upcase!
if x == 'B' then
x = 'z'
end
end
puts letters
produces this output:
$ ruby test.rb
A
B
C
D
how come inside the "each" block, the upcase! method worked, but the assignment operator inside the "if" statement did not work?
if it was as simple as "x" is a local variable in the "each" block, then wouldn't both the upcase! method and the assignment operator not stick when you exit the loop?
how can i make the the assignment operator stick even after exiting the loop?
thanks guys.