-
Notifications
You must be signed in to change notification settings - Fork 0
/
hfr-ch-10-comparable-and-enumerable.rb
59 lines (49 loc) · 1.24 KB
/
hfr-ch-10-comparable-and-enumerable.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
53
54
55
56
57
58
59
# comparable mixin (def <=> and you get to use <,>,>=,<=, between?)
class Steak
GRADE_SCORES = {
"Prime" => 3,
"Choice" => 2,
"Select" => 1
}
include Comparable
attr_accessor :grade
#No need to define >,<,<= methods
#def >(other)
# GRADE_SCORES[grade] > GRADE_SCORES[other.grade]
#end
#just define a spaceship method as below and mixin the comparable module
def <=>(other)
case grade
when GRADE_SCORES[grade] < GRADE_SCORES[other.grade]
return -1
when GRADE_SCORES[grade] == GRADE_SCORES[other.grade]
return 0
else
return 1
end
end
end
first_steak = Steak.new
first_steak.grade = "Prime"
second_steak = Steak.new
second_steak.grade = "Choice"
p first_steak > second_steak
#enumerable module as a mixin
#just define an each method
class WordSplitter
attr_accessor :string
include Enumerable
def each
string.split(" ").each { |word| yield word }
end
end
splitter = WordSplitter.new
splitter.string = "one two three four"
splitter.each do |word|
puts word
end
#enables you to work on the object as opposed to splitter.string.split(" ")
puts splitter.first
puts splitter.string.split(" ").first
puts splitter.count
puts splitter.map { |word| word + " foo" }