How to create a method object of from an singleton method?

class Foo
  def suprim;end
end

p foo = Foo.new #=> #<Foo:0x90bd908>
p foo.method(:suprim) #=> #<Method: Foo#suprim>
p Bar=foo.singleton_class #=> #<Class:#<Foo:0x90bd908>>
p foo.define_singleton_method(:hello) { "#{self}: Hello there!" } #=>
#<Proc:0x90bd2a0@/home/kirti/ruby/test.rb:8 (lambda)>
p Bar.method(:hello) #=> `method': undefined method `hello' for class
`Class' (NameError)

My question is how to create a method object of an singleton method,like
I did `foo.method(:suprim)`?

···

--
Posted via http://www.ruby-forum.com/.

Not sure if the ruby docs aren't clear enough.

Look into define_method, class_eval and instance_eval. It will be one
of those or higher ordered scoped where singleton_ is before them. Of
course you can always user eval primitive or monkey patch it all the
same.

~Stu

···

On Fri, May 17, 2013 at 2:42 PM, Love U Ruby <lists@ruby-forum.com> wrote:

class Foo
  def suprim;end
end

p foo = Foo.new #=> #<Foo:0x90bd908>
p foo.method(:suprim) #=> #<Method: Foo#suprim>
p Bar=foo.singleton_class #=> #<Class:#<Foo:0x90bd908>>
p foo.define_singleton_method(:hello) { "#{self}: Hello there!" } #=>
#<Proc:0x90bd2a0@/home/kirti/ruby/test.rb:8 (lambda)>
p Bar.method(:hello) #=> `method': undefined method `hello' for class
`Class' (NameError)

My question is how to create a method object of an singleton method,like
I did `foo.method(:suprim)`?

--
Posted via http://www.ruby-forum.com/\.

Sounds like you're looking for this:

Bar.instance_method(:hello) # => #<UnboundMethod:
#<Class:#<Foo:0x007faaf31605a8>>#hello>
Bar.instance_method(:hello).bind(foo) # => #<Method:
#<Foo:0x007faaf31605a8>.hello>
foo.method :hello # => #<Method:
#<Foo:0x007faaf31605a8>.hello>

···

On Fri, May 17, 2013 at 2:42 PM, Love U Ruby <lists@ruby-forum.com> wrote:

class Foo
  def suprim;end
end

p foo = Foo.new #=> #<Foo:0x90bd908>
p foo.method(:suprim) #=> #<Method: Foo#suprim>
p Bar=foo.singleton_class #=> #<Class:#<Foo:0x90bd908>>
p foo.define_singleton_method(:hello) { "#{self}: Hello there!" } #=>
#<Proc:0x90bd2a0@/home/kirti/ruby/test.rb:8 (lambda)>
p Bar.method(:hello) #=> `method': undefined method `hello' for class
`Class' (NameError)

My question is how to create a method object of an singleton method,like
I did `foo.method(:suprim)`?