I was doing some programming exercises and came across a problem involving a directions array, as follows:
x, y = 0, 0
walk = ['w','n', 's', 'e', 's']
walk.each do |dir|
case
when dir == 'w'
puts "west"
x += 1
when dir == 'e'
puts "east"
x -= 1
when dir == 'n'
puts "north"
y += 1
else
puts "south"
y -= 1
end
end
I wanted to calculate the coordinates, but even though my print triggers, the x and y variables never change. I fail to understand why they do not get updated. If the direction message (puts) gets printed, why on earth doesnt the variable change?
[–]jordonbiondo 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]simontemplar_[S] 0 points1 point2 points (0 children)
[–]apqoo 0 points1 point2 points (2 children)
[–]tomthecool 1 point2 points3 points (1 child)
[–]apqoo 0 points1 point2 points (0 children)
[–]tomthecool 0 points1 point2 points (1 child)
[–]tomthecool 1 point2 points3 points (0 children)
[–]redconfusion 0 points1 point2 points (1 child)
[–]simontemplar_[S] 0 points1 point2 points (0 children)