Class: UnboundMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/rebound.rb

Instance Method Summary collapse

Instance Method Details

#bind_with_indifference(obj) ⇒ Object Also known as: bind

Allows an unbound method to be bound to any object, instead of only those of the same class. Goes with original #bind method first, and if that fails, meta_def’s using #to_proc



40
41
42
# File 'lib/rebound.rb', line 40

def bind_with_indifference(obj)
  bind_without_indifference(obj) rescue class << obj; self end.class_eval(to_s(:ruby))
end

#nameObject

Simple string name. Taken from Pat Maddox’s with_context



32
33
34
# File 'lib/rebound.rb', line 32

def name
  @name ||= to_s.split("#").last.delete(">")
end

#to_procObject

eval’s and memoizes the output of Ruby2Ruby’s #to_ruby method



11
12
13
# File 'lib/rebound.rb', line 11

def to_proc
  @to_proc ||= eval(to_ruby)
end

#to_s_with_ruby(opt = nil) ⇒ Object Also known as: to_s

This is sort of ugly, but it does allow us to bind methods that take block arguments (you can’t use block arguments with blocks).



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rebound.rb', line 18

def to_s_with_ruby(opt=nil)
  (opt != :ruby) ? to_s_without_ruby : begin
    @to_s ||= begin
      res = to_ruby
      res.gsub!(/\Aproc \{ /, "def #{name}")      # Replace proc definition
      res.gsub!(/\|([^\|]*)\|\n/, "(#{'\1'})\n")  # Use method param declaration
      res.gsub!(/\}\z/, 'end')                    # Replace proc end brace
      res
    end
  end
end