-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegator.rb
52 lines (41 loc) · 983 Bytes
/
delegator.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
# http://ruby-doc.org/stdlib-1.9.3/libdoc/delegate/rdoc/SimpleDelegator.html
class Person
attr_accessor :name, :fav_color
def initialize(name, fav_color)
self.name = name
self.fav_color = fav_color
end
end
class OtherPerson
attr_accessor :name, :least_fav_color
def initialize(name, least_fav_color)
self.name = name
self.least_fav_color = least_fav_color
end
end
class AnyPerson < SimpleDelegator
def color
if __getobj__.respond_to?(:fav_color)
puts self.fav_color
elsif __getobj__.respond_to?(:least_fav_color)
puts self.least_fav_color
else
puts "I don't know this object"
end
end
def whatAmI
puts __getobj__.class.name
puts "My Methods are:"
(__getobj__.methods - Object.methods).each {|m| puts m}
end
end
p = Person.new("Liam", "Blue")
op = OtherPerson.new("George", "Red")
a1 = AnyPerson.new(p)
a1.whatAmI
puts a1.name
a1.color
a2 = AnyPerson.new(op)
a2.whatAmI
puts a2.name
a2.color