Class: Class
- Inherits:
-
Object
- Object
- Class
- Defined in:
- lib/watts/monkey_patching.rb
Overview
This is the place to stuff all of the monkey-patches.
Instance Method Summary collapse
-
#attr_new(klass, *attrs) ⇒ Object
A replacement for def x; @x ||= Y.new; end.
-
#to_instance(*ms) ⇒ Object
Has instances delegate methods to the class.
Instance Method Details
#attr_new(klass, *attrs) ⇒ Object
A replacement for def x; @x ||= Y.new; end
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/watts/monkey_patching.rb', line 14 def attr_new klass, *attrs attrs.each { |attr| ivname = "@#{attr}" define_method(attr) { ivval = instance_variable_get(ivname) return ivval if ivval instance_variable_set(ivname, klass.new) } } end |
#to_instance(*ms) ⇒ Object
Has instances delegate methods to the class.
5 6 7 8 9 10 11 |
# File 'lib/watts/monkey_patching.rb', line 5 def to_instance *ms ms.each { |m| define_method(m) { |*a| self.class.send(m, *a) } } end |