From: Josh Cheek Date: 2013-05-18T07:39:45+09:00 Subject: Re: how to create a method object of from an singleton method? --089e0122866cbe8ddf04dcf1a846 Content-Type: text/plain; charset=ISO-8859-1 On Fri, May 17, 2013 at 2:42 PM, Love U Ruby wrote: > class Foo > def suprim;end > end > > p foo = Foo.new #=> # > p foo.method(:suprim) #=> # > p Bar=foo.singleton_class #=> #> > p foo.define_singleton_method(:hello) { "#{self}: Hello there!" } #=> > # > 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)`? > > Sounds like you're looking for this: Bar.instance_method(:hello) # => #>#hello> Bar.instance_method(:hello).bind(foo) # => #.hello> foo.method :hello # => #.hello> --089e0122866cbe8ddf04dcf1a846 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Fri, May 17, 2013 at 2:42 PM, Love U Ruby <lists@ruby-forum.com= > wrote:
class Foo
=A0 def suprim;end
end

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

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


Sounds like you're looking for this:
<= br>
Bar.instance_method(:hello) =A0 =A0 =A0 =A0 =A0 =A0# =3D= > #<UnboundMethod: #<Class:#<Foo:0x007faaf31605a8>>#hello= >
Bar.instance_method(:hello).bind(foo) =A0# =3D> #<Method: #<F= oo:0x007faaf31605a8>.hello>
foo.method :hello =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# =3D> #<Method: #<Foo:0x007faaf316= 05a8>.hello>

--089e0122866cbe8ddf04dcf1a846--