Skip to content
Permalink
Browse files
implement UnboundMethod#for_define_method
  • Loading branch information
tak1n committed Feb 5, 2016
1 parent 2e501b5 commit 3f9756bb959248e15d63ccc5dca03df9a746d851
Showing with 19 additions and 24 deletions.
  1. +18 −0 core/method.rb
  2. +1 −24 core/module.rb
@@ -338,4 +338,22 @@ def super_method

return nil
end

def for_define_method(name, klass)
Rubinius::Type.bindable_method? self.defined_in, klass

if @executable.kind_of? Rubinius::DelegatedMethod
code = @executable
scope = nil
else
code = Rubinius::DelegatedMethod.new(name, :call_on_instance, self, true)
if @executable.kind_of? Rubinius::CompiledCode
scope = @executable.scope
else
scope = nil
end
end

[code, scope]
end
end
@@ -442,30 +442,7 @@ def define_method(name, meth = undefined, &prc)

name = Rubinius::Type.coerce_to_symbol name

case meth
when Proc
code, scope = meth.for_define_method(name, nil)
when Method
code, scope = meth.for_define_method(name, self.class)
when UnboundMethod
Rubinius::Type.bindable_method? meth.defined_in, self.class

exec = meth.executable
# Same reasoning as above.
if exec.kind_of? Rubinius::DelegatedMethod
code = exec
scope = nil
else
code = Rubinius::DelegatedMethod.new(name, :call_on_instance, meth, true)
if exec.kind_of? Rubinius::CompiledCode
scope = exec.scope
else
scope = nil
end
end
else
raise TypeError, "wrong argument type #{meth.class} (expected Proc/Method)"
end
code, scope = meth.for_define_method(name, self.class)

Rubinius.add_method name, code, self, scope, 0, :public

0 comments on commit 3f9756b

Please sign in to comment.