Just wondering why
def move
world_switch(@pos_X += 1, @pos_X -= 1, @pos_Y += 1, @pos_Y -= 1)
end
def world_switch(do_on_north, do_on_south, do_on_east, do_on_west)
case @facing # => 'NORTH'
when 'NORTH'
puts do_on_north # => 1
do_on_north
when 'SOUTH'
do_on_south
when 'EAST'
do_on_east
when 'WEST'
do_on_west
end
end
Calling world_switch:
robot = Robot.new(0, 0, 'NORTH')
robot.move
puts robot.instance_variable_get("@pos_X") #=> 0
results in changing nothing, I would like to increase or decrease instance variable @pos_X or @pos_Y
This is my initialize method
def initialize(pos_X, pos_Y, facing)
@pos_X, @pos_Y, @facing = pos_X, pos_Y, facing
end
and that’s how I create an instance of the class robot = Robot.new(0, 0, 'NORTH')
All help will be appreciated
The explanation for the current behaviour is as Chowlett described, but did you intend for your
@pos_X += 1, @pos_X -= 1etc inmoveto be blocks of code and then for exactly one of these to be called fromworld_switchdepending on which way the robot is facing?If so, move needs to be declared like this
and then in
world_switchyou can do something like