Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Define direction. Turn_right method to return the next value in the a…
…rray. Add direction_spec
- Loading branch information
1 parent
c41c004
commit 92324ac
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Direction | ||
class Direction | ||
attr_reader :start_direction | ||
|
||
def initialize(start_direction) | ||
@start_direction = start_direction | ||
|
||
@directions_array = %w[WEST NORTH EAST SOUTH] | ||
end | ||
|
||
def turn_right(current_direction) | ||
puts 'turn right' | ||
|
||
# does the current direction exist? | ||
return unless @directions_array.include?(current_direction) | ||
|
||
i = @directions_array.index { |e| e == current_direction } | ||
|
||
unless current_direction == @directions_array.last | ||
i += 1 | ||
|
||
new_direction = @directions_array.fetch(i) | ||
puts "New Direction: #{new_direction}" | ||
|
||
return new_direction | ||
end | ||
|
||
puts "New Direction: #{@directions_array.first}" | ||
end | ||
end | ||
|
||
direction = Direction.new('NORTH') | ||
|
||
puts "Starting at #{direction.start_direction}" | ||
current_direction = direction.start_direction | ||
|
||
direction.turn_right(current_direction) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'rspec' | ||
require 'spec_helper' | ||
require './lib/direction' | ||
|
||
describe Direction do | ||
describe '#initialize' do | ||
it 'should have a start_direction attribute which is a String' do | ||
instance = Direction.new('NORTH') | ||
|
||
expect(instance.start_direction).to be_a String | ||
end | ||
end | ||
|
||
describe '#turn_right' do | ||
it 'should return the next value in the directions array' do | ||
instance = Direction.new('NORTH') | ||
current_direction = 'NORTH' | ||
|
||
expect(instance.turn_right(current_direction)).to be_a String | ||
expect(instance.turn_right(current_direction)).to eq 'EAST' | ||
end | ||
|
||
it 'should return nil if the current_direction is empty' do | ||
instance = Direction.new('NORTH') | ||
current_direction = '' | ||
|
||
expect(instance.turn_right(current_direction)).to be_nil | ||
end | ||
end | ||
end |