Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- What is the best way to over-ride methods in an instance?
- class DataWrapper
- attr_reader :foo, :bar
- end
- case :xml
- @data1.to_xml.foo #foo = 4
- case :web_table
- @data1.to_web_table.foo #foo = "four"
- array.each{ |o| o.extend(MyModule) }
- Nib = Struct.new :val do
- def to_s
- value # Must be implemented by instance/subclass
- end
- end
- module Nib::Precise
- def value; "%.1f" % val; end
- end
- module Nib::Rough
- def value; val.round.to_s; end
- end
- module Nib::Ballpark
- def value; ((val/10).round * 10).to_s; end
- end
- nibs = [ Nib.new(33.7), Nib.new(29.1) ]
- nibs.each{ |n| n.extend(Nib::Precise) }
- p nibs.join(", ") #=> "33.7, 29.1"
- nibs.each{ |n| n.extend(Nib::Rough) }
- p nibs.join(", ") #=> "34, 29"
- nibs.each{ |n| n.extend(Nib::Ballpark) }
- p nibs.join(", ") #=> "30, 30"
- case :xml
- @data1.foo.to_xml #foo = 4
- case :web_table
- @data1.foo.to_web_table #foo = "four"
- ....
Add Comment
Please, Sign In to add comment