-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter_class_reopen.rb
More file actions
49 lines (48 loc) · 937 Bytes
/
adapter_class_reopen.rb
File metadata and controls
49 lines (48 loc) · 937 Bytes
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
class TextObject
attr_reader :text, :size_inches, :color
def initialize(text, size_inches, color)
@text = text
@size_inches = size_inches
@color = color
end
end
class BritishTextObject
attr_reader :string, :size_mm, :colour
def initialize(string, size_mm, colour)
@string = string
@size_mm = size_mm
@colour = colour
end
end
## Using inheritance based adapter approach
#class BritishTextObjectAdapter < TextObject
# def initialize(bto)
# @bto = bto
# end
# def text
# @bto.string
# end
# def size_inches
# @bto.size_mm/25.4
# end
# def color
# @bto.colour
# end
#end
# Ruby allows reopening classes or objects
class BritishTextObject
def color
colour
end
def text
string
end
def size_inches
size_mm/25.4
end
end
bto = BritishTextObject.new('string', 50.84, 'Red')
#bto = BritishTextObjectAdapter.new(bto1)
puts bto.text
puts bto.color
puts bto.size_inches