-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymorphism.rb
57 lines (46 loc) · 1.08 KB
/
polymorphism.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
# Inheritance
class Membership
def initialize(id, name, status)
@id = id
@name = name
@status = status
end
def features
puts "You are able to access #{@status} features"
end
end
class Silver < Membership
def features
puts "Congratulations #{@name}! you've been promoted to #{@status} membership. You can use these additional benefits"
end
end
class Gold < Membership
def features
puts "Congratulations #{@name}! you've been promoted to #{@status} membership. These are the best benefits that you can get"
end
end
customer1 = Silver.new("1", "Ian", "Silver")
customer1.features
customer2 = Gold.new("2", "Ralph", "Gold")
customer2.features
# Duck Typing
class Membership
def status(membership)
membership.status
end
end
class Silver
def status
puts "You are a Silver member"
end
end
class Gold
def status
puts "You are a Gold member"
end
end
customer = Membership.new
customer1 = Silver.new
customer.status(customer1)
customer2 = Gold.new
customer.status(customer2)