Class: Module
Direct Known Subclasses
Instance Method Summary collapse
-
#attr_reader?(*attrs) ⇒ void
Generate accessor method with question mark.
-
#attr_setter(*attrs) ⇒ void
Generate attribute setter.
-
#private_constant(*args) ⇒ Object
HACK: private_constant was introduced in 1.9.3.
-
#redefine_method(name) { ... } ⇒ void
Redefine a module method.
Instance Method Details
#attr_reader?(*attrs) ⇒ void
This method returns an undefined value.
Generate accessor method with question mark
10 11 12 13 14 |
# File 'lib/olelo/extensions.rb', line 10 def attr_reader?(*attrs) attrs.each do |a| module_eval "def #{a}?; !!@#{a}; end" end end |
#attr_setter(*attrs) ⇒ void
This method returns an undefined value.
Generate attribute setter
A setter accepts an argument to set a value. It acts as a getter without an argument.
25 26 27 28 29 30 31 32 33 |
# File 'lib/olelo/extensions.rb', line 25 def attr_setter(*attrs) code, made = '', [] attrs.each do |a| code << "def #{a}(*a); a.size > 0 ? (@#{a}=a[0]; self) : @#{a} end\n" made << a.to_sym end module_eval(code) made end |
#private_constant(*args) ⇒ Object
HACK: private_constant was introduced in 1.9.3
3 |
# File 'lib/olelo/extensions.rb', line 3 def private_constant(*args); end |
#redefine_method(name) { ... } ⇒ void
This method returns an undefined value.
Redefine a module method
Replaces alias_method_chain and allows to call overwritten method via super.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/olelo/extensions.rb', line 44 def redefine_method(name, &block) if instance_methods(false).any? {|x| x.to_s == name.to_s } method = instance_method(name) mod = Module.new do define_method(name) {|*args| method.bind(self).call(*args) } end remove_method(name) include(mod) end include(Module.new { define_method(name, &block) }) end |