Skip to content

Commit

Permalink
* Direction's parent is Object now, not String.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledestin committed Apr 3, 2014
1 parent 9e1839b commit 641c22c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
24 changes: 21 additions & 3 deletions direction.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
class Robot
class Direction < String
class Direction
ALL_STR = %w(NORTH EAST SOUTH WEST)

def self.Direction direction
return direction if direction.is_a? Direction

raise ArgumentError, "#{direction}: unknown direction" \
unless ALL_STR.include? direction

Direction.new direction
end

attr_reader :name

def initialize name
@name = name
end

NORTH = Direction 'NORTH'
EAST = Direction 'EAST'
SOUTH = Direction 'SOUTH'
WEST = Direction 'WEST'
ALL = [NORTH, EAST, SOUTH, WEST].map! { |d| d.freeze }

def == other
@name == other.name
end

def next
case self
when NORTH then EAST
Expand All @@ -25,7 +37,8 @@ def next
end

def next!
replace self.next
@name = self.next.name
self
end

def prev
Expand All @@ -38,7 +51,12 @@ def prev
end

def prev!
replace self.prev
@name = prev.name
self
end

def to_s
@name
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions state.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
class Robot
State = Struct.new(:x, :y, :direction) do
def initialize x, y, direction
self.x, self.y, self.direction =
Integer(x), Integer(y), Direction(direction)
end

def to_s
[x, y, direction].join ','
end
Expand Down

0 comments on commit 641c22c

Please sign in to comment.