-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes and Inheritance_Basic.rb
69 lines (52 loc) · 2.06 KB
/
classes and Inheritance_Basic.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
60
61
62
63
64
65
66
#Author Gurpreet Singh
class Song1 #class A
def initialize(name,artist,duration)
@name=name
@artist=artist
@duration=duration
end
#Changing the definition of to_s to give the contents and not object code
def to_s # Now it dosen't throw the object code
"Song: #@name #@artist (#@duration)" #[#@lyrics]"
end
end
#Class B inheriting class A
class Karokie < Song1 #“< Song1 on the class definition line tells Ruby that a Karaoke is a subclass of Song1, i.e song1 is super class
def initialize(name,artist,duration,lyrics)
super(name,artist,duration)
@lyrics=lyrics
end
#Here I have decoupled the definition of to_s as the lyrics is part of karokie
def to_s
super+" [#@lyrics]"
end
end
#Note: A Ruby class has only one direct parent, so Ruby is a single inheritance language.
#Class C inheriting class B
class Instumental < Karokie
def initialize(name,artist,duration,lyrics,music_type)
super(name,artist,duration,lyrics)
@music_type=music_type
end
#Here I have decoupled the definition of to_s as music is part of instumental
def to_s
super+" [#@music_type]"
end
end
# Main starts here:
a= Song1.new("Bicylops", "Fleck", 260)
b= a.to_s
puts "With .to_s: \t #{b}"
c= a.inspect
puts "With inspect: \t #{c}"
d= Karokie.new('My Way','Sinatra',225,'And now the')
e=d.to_s # First it looks in class B if the method is not present there then moves to parent ans so on.
puts "Calling class B: Karokie inheriting class A: Song1: \t #{e}"
f= Instumental.new('All of me', 'Chain Smokers',300, 'All of me loves all of you','Soft')
g=f.to_s
puts "Calling class C: Instumental inheriting class B: Karokie: \t #{f.to_s}"
# Run in RubyMine---------------> OUTPUT
# With .to_s: Song: Bicylops Fleck (260)
# With inspect: #<Song1:0x007fecce03f378 @name="Bicylops", @artist="Fleck", @duration=260>
# calling class B: Karokie inheriting class A: Song1: Song: My Way Sinatra (225) [And now the]
# Calling class C: Instumental inheriting class B: Karokie: Song: All of me Chain Smokers (300) [All of me loves all of you] [Soft]