Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3031
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Robert Klemme <shortcutter@googlemail.com> |
| Newsgroups | comp.lang.ruby |
| Subject | Re: looking for an "inversion" pattern |
| Date | Sat, 16 Apr 2011 23:40:21 +0200 |
| Lines | 50 |
| Message-ID | <90ugngF3ieU1@mid.individual.net> (permalink) |
| References | <d0c168880ff1c65dd84fc6d70778ddb2@ruby-forum.com> <c9351f67cbcb09e73dec51721abdc873@ruby-forum.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net R2nN+wk8vevezr64Jg0BagcyXD474eUf4BjtEb9a1U7KVYNR0= |
| Cancel-Lock | sha1:6ZCDIQ9Klcq6fetMpJdxfK5wOic= |
| User-Agent | Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.15) Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9 |
| In-Reply-To | <c9351f67cbcb09e73dec51721abdc873@ruby-forum.com> |
| X-Antivirus | avast! (VPS 110416-0, 16.04.2011), Outbound message |
| X-Antivirus-Status | Clean |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.ruby:3031 |
Show key headers only | View raw
On 16.04.2011 23:14, Brian Candler wrote:
> Fearless Fool wrote in post #992929:
>> I'm sure there's a clean way to do this in Ruby, but I haven't figured
>> it out.
>>
>> I'd like to create a method +foo+ that transforms:
>>
>> my_obj.foo.some_method(*args)
>>
>> to
>>
>> MyClass.some_method(my_obj, *args)
>
> Not sure, but would bound or unbound methods help you?
>
> bound_method = my_obj.method(:foo)
> bound_method.call(*args)
>
> unbound_method = my_obj.class.instance_method(:foo)
> unbound_method.bind(my_obj).call(*args)
You can only bind to instances of the original type. You cannot use
#unbind and #bind to let a method be called on a different type. But
this would be necessary for the OP's requirement to be fulfilled.
irb(main):001:0> class Foo
irb(main):002:1> def self.bar;123;end
irb(main):003:1> end
=> nil
irb(main):004:0> m=Foo.method(:bar).unbind
=> #<UnboundMethod: #<Class:Foo>#bar>
irb(main):005:0> f=Foo.new
=> #<Foo:0x105a24a4>
irb(main):006:0> m.bind(f)
TypeError: singleton method called for a different object
from (irb):6:in `bind'
from (irb):6
from /usr/local/bin/irb19:12:in `<main>'
irb(main):007:0> m.bind(Foo)
=> #<Method: Foo.bar>
irb(main):008:0> m.bind(Foo).call
=> 123
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.ruby | Previous | Next — Previous in thread | Find similar | Unroll thread
looking for an "inversion" pattern Fearless Fool <r@alum.mit.edu> - 2011-04-15 00:16 -0500
Re: looking for an "inversion" pattern Fearless Fool <r@alum.mit.edu> - 2011-04-15 01:27 -0500
Re: looking for an "inversion" pattern Robert Klemme <shortcutter@googlemail.com> - 2011-04-15 04:24 -0500
Re: looking for an "inversion" pattern Fearless Fool <r@alum.mit.edu> - 2011-04-15 10:40 -0500
Re: looking for an "inversion" pattern Robert Klemme <shortcutter@googlemail.com> - 2011-04-16 15:29 +0200
Re: looking for an "inversion" pattern Kevin Mahler <kevin.mahler@yahoo.com> - 2011-04-15 02:14 -0500
Re: looking for an "inversion" pattern Fearless Fool <r@alum.mit.edu> - 2011-04-15 02:43 -0500
Re: looking for an "inversion" pattern Kevin Mahler <kevin.mahler@yahoo.com> - 2011-04-15 08:44 -0500
Re: looking for an "inversion" pattern 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-16 13:20 -0500
Re: looking for an "inversion" pattern 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-16 13:43 -0500
Re: looking for an "inversion" pattern Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-15 02:45 -0500
Re: looking for an "inversion" pattern Brian Candler <b.candler@pobox.com> - 2011-04-16 16:14 -0500
Re: looking for an "inversion" pattern Robert Klemme <shortcutter@googlemail.com> - 2011-04-16 23:40 +0200
csiph-web