Permalink
Browse files

Define direction. Turn_right method to return the next value in the a…

…rray. Add direction_spec
  • Loading branch information...
SelenaSmall committed Aug 26, 2017
1 parent c41c004 commit 92324acfd6e5290c5a90dc040138540ceb8bf8a5
Showing with 67 additions and 0 deletions.
  1. +37 −0 lib/direction.rb
  2. +30 −0 spec/direction_spec.rb
@@ -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)
@@ -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

0 comments on commit 92324ac

Please sign in to comment.