Sha256: 46cb76e2eaf1a4540ad57601341b0068cb745393eba6171bdb7d29f758c107d9

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

class Proc

  # Returns the Proc converted to an UnboundMethod within the the given Class
  #
  #   class A
  #     def a
  #       "a"
  #     end
  #   end
  #
  #   um_a = A.instance_method( :a )   # => #<UnboundMethod: A#a>
  #   um_a.bind( A.new ).call          # => "a"
  #
  #   b = lambda do "b" end
  #   um_b = b.to_unbound_method( A )  # => #<UnboundMethod: A#_um_from_proc>
  #   um_b.bind( A.new ).call          # => "b"
  #
  def to_unbound_method( klass )
    raise ArgumentError.new( "Only class objects allowed in parameter list" 
                                  ) unless klass.kind_of?( Class )

    proc_object = self
    klass.class_eval do
      define_method( :_um_from_proc, &proc_object )
    end

    unbound_method = klass.instance_method( :_um_from_proc )

    klass.class_eval do
      undef_method( :_um_from_proc )
    end

    unbound_method
  end

  # joins to blocks into a new one, that forwards on excution the given 
  # arguments.
  #
  #   a = lambda do print "a" end
  #   b = lambda do print "b" end
  #
  #   ab = a + b
  #
  #   ab.call  # => "ab"
  #
  def +( other_proc )
    this_proc = self
    lambda do | *arguments |
      this_proc.call( *arguments )
      other_proc.call( *arguments )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contextr-0.0.2 lib/core_ext/proc.rb
contextr-0.0.3 lib/core_ext/proc.rb