class MyToyRobot::Command
Public Class Methods
command_type(command)
click to toggle source
# File lib/my_toy_robot/command.rb, line 12 def self.command_type(command) case true when command.include?("PLACE") then :place when command.include?("MOVE") then :move when command.include?("LEFT") then :turn_left when command.include?("RIGHT") then :turn_right when command.include?("REPORT") then :report end end
get_match_obj(command)
click to toggle source
# File lib/my_toy_robot/command.rb, line 3 def self.get_match_obj(command) place_match = /\APLACE (?<x>\d+),(?<y>\d+),(?<direction>\w+)\Z/.match(command) move_match = /\AMOVE\Z/.match(command) left_match = /\ALEFT\Z/.match(command) right_match = /\ARIGHT\Z/.match(command) report_match = /\AREPORT\Z/.match(command) [place_match, move_match, left_match, right_match, report_match].compact[0] end
process(command)
click to toggle source
# File lib/my_toy_robot/command.rb, line 22 def self.process(command) match = self.get_match_obj(command) unless match return [:invalid, command] end type = self.command_type(command) if type == :place [type, match[:x].to_i, match[:y].to_i, match[:direction]] else [type] end end