Module: Monkey::Ext::Object
- Defined in:
- lib/monkey/ext/object.rb
Instance Method Summary collapse
- #define_singleton_method(name, &block) ⇒ Object
-
#instance_yield(block = nil, &alternate_block) ⇒ Object
Behaves like instance_eval or yield depending on whether a block takes an argument or not.
- #metaclass ⇒ Object
- #metaclass_eval(&block) ⇒ Object
- #singleton_class_eval(&block) ⇒ Object
Instance Method Details
#define_singleton_method(name, &block) ⇒ Object
32 33 34 |
# File 'lib/monkey/ext/object.rb', line 32 def define_singleton_method(name, &block) singleton_class_eval { define_method(name, &block) } end |
#instance_yield(block = nil, &alternate_block) ⇒ Object
Behaves like instance_eval or yield depending on whether a block takes an argument or not.
class Foo
define_method(:foo) { 42 }
end
Foo.new.instance_yield { foo } # => 42
Foo.new.instance_yield { |c| c.foo } # => 42
Also, you can pass a proc directly:
block = proc { }
instance_yield(block)
21 22 23 24 25 26 |
# File 'lib/monkey/ext/object.rb', line 21 def instance_yield(block = nil, &alternate_block) raise ArgumentError, "too many blocks given" if block && alternate_block block ||= alternate_block raise LocalJumpError, "no block given (yield)" unless block block.arity > 0 ? yield(self) : instance_eval(&block) end |
#metaclass ⇒ Object
36 37 38 39 |
# File 'lib/monkey/ext/object.rb', line 36 def warn "DEPRECATION WARNING: #metaclass will be removed, use #singleton_class (#{caller})" singleton_class end |
#metaclass_eval(&block) ⇒ Object
41 42 43 44 |
# File 'lib/monkey/ext/object.rb', line 41 def (&block) warn "DEPRECATION WARNING: #metaclass_eval will be removed, use #singleton_class_eval (#{caller})" singleton_class_eval(&block) end |
#singleton_class_eval(&block) ⇒ Object
28 29 30 |
# File 'lib/monkey/ext/object.rb', line 28 def singleton_class_eval(&block) singleton_class.class_eval(&block) end |