Sha256: 9d99b54f6cd2728fbdb876843217128134ae547dbbffaaeefc5ee9ea7c091ddf

Contents?: true

Size: 1.31 KB

Versions: 55

Compression:

Stored size: 1.31 KB

Contents

class Object
  # copy from active_support
  def delegate(*methods)
    options = methods.pop
    unless options.is_a?(Hash) && to = options[:to]
      raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
    end

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

class Object
  unless defined? instance_exec # 1.9
    module InstanceExecMethods #:nodoc:
    end
    include InstanceExecMethods

    # Evaluate the block with the given arguments within the context of
    # this object, so self is set to the method receiver.
    #
    # From Mauricio's http://eigenclass.org/hiki/bounded+space+instance_exec
    def instance_exec(*args, &block)
      begin
        old_critical, Thread.critical = Thread.critical, true
        n = 0
        n += 1 while respond_to?(method_name = "__instance_exec#{n}")
        InstanceExecMethods.module_eval { define_method(method_name, &block) }
      ensure
        Thread.critical = old_critical
      end

      begin
        send(method_name, *args)
      ensure
        InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
      end
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
ruco-0.1.8 lib/ruco/core_ext/object.rb
ruco-0.1.7 lib/ruco/core_ext/object.rb
ruco-0.1.6 lib/ruco/core_ext/object.rb
ruco-0.1.5 lib/ruco/core_ext/object.rb
ruco-0.1.4 lib/ruco/core_ext/object.rb
ruco-0.1.3 lib/ruco/core_ext/object.rb
ruco-0.1.2 lib/ruco/core_ext/object.rb
ruco-0.1.1 lib/ruco/core_ext/object.rb
ruco-0.1.0 lib/ruco/core_ext/object.rb
ruco-0.0.56 lib/ruco/core_ext/object.rb
ruco-0.0.55 lib/ruco/core_ext/object.rb
ruco-0.0.54 lib/ruco/core_ext/object.rb
ruco-0.0.53 lib/ruco/core_ext/object.rb
ruco-0.0.52 lib/ruco/core_ext/object.rb
ruco-0.0.51 lib/ruco/core_ext/object.rb
ruco-0.0.50 lib/ruco/core_ext/object.rb
ruco-0.0.49 lib/ruco/core_ext/object.rb
ruco-0.0.48 lib/ruco/core_ext/object.rb
ruco-0.0.47 lib/ruco/core_ext/object.rb
ruco-0.0.46 lib/ruco/core_ext/object.rb