Class: Object

Inherits:
BasicObject
Defined in:
lib/easy_partials/object_additions.rb

Instance Method Summary collapse

Instance Method Details

#meta_def(name, &block) ⇒ Object

Define an instance method using a symbol for the method name and a block for the method contents. This makes it so a closure can be used to define a singleton method.



12
13
14
# File 'lib/easy_partials/object_additions.rb', line 12

def meta_def(name, &block)
  metaclass.send :define_method, name, &block
end

#meta_def_with_block(name, &block) ⇒ Object

Define an instance method using a block that will accept a block as the first parameter, and the rest of the arguments as the second. This requires the block to have the first parameter act as the block.

For example: o.meta_def_with_block(:say_hi) { |block, name|

block.call
puts "hello #{name}!"

}

Will create a method that can be invoked as such: o.say_hi(“Mike”) { puts “Saying hi:” }

Implicitly, 2 methods are created… __real__say_hi, which is created using the given block, and say_hi, which will accept the arguments and block and pass them to __real__say_hi in reverse order.



34
35
36
37
38
39
40
41
# File 'lib/easy_partials/object_additions.rb', line 34

def meta_def_with_block(name, &block)
  meta_def "__real__#{name}".to_sym, &block
  metaclass.instance_eval "
    def #{name}(*args, &block)
      __real__#{name} block, *args
    end
  "
end

#metaclassObject

Obtain the metaclass of this object instance.



3
4
5
6
7
# File 'lib/easy_partials/object_additions.rb', line 3

def metaclass
  class << self
    self
  end
end