class MyToyRobot::Robot

Constants

DIRECTIONS

Attributes

direction[R]
east[R]
north[R]

Public Class Methods

new(east = 0, north = 0, direction = "NORTH") click to toggle source
# File lib/my_toy_robot/robot.rb, line 5
def initialize(east = 0, north = 0, direction = "NORTH") 
  @east = east
  @north = north
  @direction = direction
end

Public Instance Methods

move() click to toggle source
# File lib/my_toy_robot/robot.rb, line 27
def move 
  send("move_#{@direction.downcase}")
end
move_east() click to toggle source
# File lib/my_toy_robot/robot.rb, line 11
def move_east 
  @east += 1
end
move_north() click to toggle source
# File lib/my_toy_robot/robot.rb, line 19
def move_north 
  @north += 1
end
move_south() click to toggle source
# File lib/my_toy_robot/robot.rb, line 23
def move_south 
  @north -= 1
end
move_west() click to toggle source
# File lib/my_toy_robot/robot.rb, line 15
def move_west 
  @east -= 1
end
next_move() click to toggle source
# File lib/my_toy_robot/robot.rb, line 47
def next_move 
  case @direction
  when "NORTH"
    [@east, @north + 1]
  when "SOUTH"
    [@east, @north - 1]
  when "EAST"
    [@east + 1, @north]
  when "WEST"
    [@east - 1, @north]
  end 
end
report() click to toggle source
# File lib/my_toy_robot/robot.rb, line 39
def report 
  {
    east: @east,
    north: @north,
    direction: @direction
  }
end
turn_left() click to toggle source
# File lib/my_toy_robot/robot.rb, line 31
def turn_left 
  turn(:left)
end
turn_right() click to toggle source
# File lib/my_toy_robot/robot.rb, line 35
def turn_right 
  turn(:right)
end

Private Instance Methods

turn(turn_direction) click to toggle source
# File lib/my_toy_robot/robot.rb, line 62
def turn(turn_direction)
  index = DIRECTIONS.index(@direction)
  rotations = turn_direction == :right ? 1 : -1
  @direction = DIRECTIONS.rotate(rotations)[index]
end