Skip to content

Commit

Permalink
implement UnboundMethod#for_define_method
Browse files Browse the repository at this point in the history
  • Loading branch information
tak1n committed Feb 5, 2016
1 parent 2e501b5 commit 3f9756b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
18 changes: 18 additions & 0 deletions core/method.rb
Expand Up @@ -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
25 changes: 1 addition & 24 deletions core/module.rb
Expand Up @@ -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

Expand Down

0 comments on commit 3f9756b

Please sign in to comment.