Class: Module

Inherits:
Object show all
Defined in:
lib/core_ext/module.rb

Instance Method Summary collapse

Instance Method Details

#delegate(*methods) ⇒ Object

Raises:

  • (ArgumentError)


2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/core_ext/module.rb', line 2

def delegate(*methods)
  options = methods.pop
  raise ArgumentError, "Delegation needs a target." unless options.is_a?(Hash) && to = options[:to]

  methods.each do |method|
    module_eval(<<-EOS, "(__DELEGATION__)", 1)
      def #{method}(*args, &block)
        #{to}.__send__(#{method.inspect}, *args, &block)
      end
    EOS
  end
end