-
Notifications
You must be signed in to change notification settings - Fork 0
/
square.rb
52 lines (43 loc) · 883 Bytes
/
square.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Square
attr_reader :file, :rank
def initialize coordinate
@file = coordinate[0]
@rank = coordinate[1]
end
def == other
if other.respond_to?(:coordinate)
self.coordinate == other.coordinate
end
end
alias :eql? :==
def coordinate
@file + @rank
end
def hash
coordinate.hash
end
def step file_step, rank_step
new_file = step_char @file, file_step
new_rank = step_char @rank, rank_step
Square.new new_file + new_rank
end
def step_char char, step
(char.ord + step).chr
end
private :step_char
def each_with_step file_step, rank_step, limit = nil
next_square = self
count = 0
Enumerator.new do |y|
while within_limit(limit, count)
count += 1
next_square = next_square.step(file_step, rank_step)
y << next_square
end
end
end
def within_limit limit, index
return true if limit.nil?
limit > index
end
end