-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhfr-ch-9-mixins.rb
54 lines (41 loc) · 1013 Bytes
/
hfr-ch-9-mixins.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
module AcceptsComments
def comments
@comments ||= []
#@comments belongs to the instance of the class being mixed into
#a new one is set-up each time an object is created
end
def add_comment(comment)
comments << comment
#comments calls the comments method above (attr_reader)
end
end
class Clip
include AcceptsComments #mixin
def play
puts "Playing #{object_id}"
end
end
class Video < Clip #subclass of clip
attr_reader :resolution
#sets up a reader, does nothing as resolution is nil
end
class Song < Clip
attr_reader :beats_per_minute
end
video = Video.new
video.add_comment("Cool slow motion effect!")
video.add_comment("Weird ending.") #adds to the comment above
video_2 = Video.new
song = Song.new
song.add_comment("Awesome beat.")
p video.comments, video.resolution, song.comments, video_2.comments
puts "*" * 50
module MyModule
def my_method
"Hello from my method!"
end
end
class MyClass
include MyModule
end
p ashik = MyClass.new.my_method