Skip to content

Commit 92324ac

Browse files
committed
Define direction. Turn_right method to return the next value in the array. Add direction_spec
1 parent c41c004 commit 92324ac

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

lib/direction.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Direction
2+
class Direction
3+
attr_reader :start_direction
4+
5+
def initialize(start_direction)
6+
@start_direction = start_direction
7+
8+
@directions_array = %w[WEST NORTH EAST SOUTH]
9+
end
10+
11+
def turn_right(current_direction)
12+
puts 'turn right'
13+
14+
# does the current direction exist?
15+
return unless @directions_array.include?(current_direction)
16+
17+
i = @directions_array.index { |e| e == current_direction }
18+
19+
unless current_direction == @directions_array.last
20+
i += 1
21+
22+
new_direction = @directions_array.fetch(i)
23+
puts "New Direction: #{new_direction}"
24+
25+
return new_direction
26+
end
27+
28+
puts "New Direction: #{@directions_array.first}"
29+
end
30+
end
31+
32+
direction = Direction.new('NORTH')
33+
34+
puts "Starting at #{direction.start_direction}"
35+
current_direction = direction.start_direction
36+
37+
direction.turn_right(current_direction)

spec/direction_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/direction'
4+
5+
describe Direction do
6+
describe '#initialize' do
7+
it 'should have a start_direction attribute which is a String' do
8+
instance = Direction.new('NORTH')
9+
10+
expect(instance.start_direction).to be_a String
11+
end
12+
end
13+
14+
describe '#turn_right' do
15+
it 'should return the next value in the directions array' do
16+
instance = Direction.new('NORTH')
17+
current_direction = 'NORTH'
18+
19+
expect(instance.turn_right(current_direction)).to be_a String
20+
expect(instance.turn_right(current_direction)).to eq 'EAST'
21+
end
22+
23+
it 'should return nil if the current_direction is empty' do
24+
instance = Direction.new('NORTH')
25+
current_direction = ''
26+
27+
expect(instance.turn_right(current_direction)).to be_nil
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)