[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Injecting methods from one class into another.

George Moschovitis

1/25/2005 1:09:00 PM

Hello everyone,

I am wondering if the following is possible in Ruby:

class Source
def my_method
puts 'IT WORKS'
end
end

class Dest
# no method
end

inject_method(Source, :my_method, Dest)

d = Dest.new
d.my_method # => IT WORKS

Is it possible to write 'inject_method' in Ruby ?
Thanks in advance,
George

27 Answers

ts

1/25/2005 1:19:00 PM

0

>>>>> "G" == George Moschovitis <george.moschovitis@gmail.com> writes:

G> inject_method(Source, :my_method, Dest)

What do you expect with this ?

inject_method(Array, :[], Hash)


Guy Decoux






George Moschovitis

1/25/2005 2:04:00 PM

0

> What do you expect with this ?
> inject_method(Array, :[], Hash)

this is an artificial example. I expect Hash's [] to be replaced
with Array's version thus creating a bug.

however, I would like to use it carefully. My question was: is this
possible?

-g.

ts

1/25/2005 2:15:00 PM

0

>>>>> "G" == George Moschovitis <george.moschovitis@gmail.com> writes:

G> however, I would like to use it carefully. My question was: is this
G> possible?

Well, you can use #instance_method and #bind but ruby will make the test

uln% ruby -e 'Array.instance_method(:[]).bind(Hash.new)'
-e:1:in `bind': bind argument must be an instance of Array (TypeError)
from -e:1
uln%



Guy Decoux



Szymon Drejewicz

1/25/2005 2:38:00 PM

0

Why you don't want to use module?

module CustomFunctionality
def my_method
puts 'IT WORKS'
end
end

class Source
include CustomFunctionality
end

class Dest
include CustomFunctionality

def another_method
puts 'HELLO'
end
end

d = Dest.new

d.my_method # => IT WORKS


--
Szymon Drejewicz
Yet Another Pragmatic Rubist :-)

George Moschovitis

1/25/2005 3:16:00 PM

0

Well, I know about modules, this is not what I want.

I want to inject only ONE method from a class containing multiple
methods into another.

-g.

Gennady

1/25/2005 3:32:00 PM

0

George Moschovitis wrote:
> Well, I know about modules, this is not what I want.
>
> I want to inject only ONE method from a class containing multiple
> methods into another.
>
> -g.
>
>
Object methods do not work in vacuum, they usually use instance
variables to reffer to the object's state, possibly changing it
(remember encapsulation ;-)?). How would you expect your
inject_method() deal with it?

Gennady.


George Moschovitis

1/25/2005 3:35:00 PM

0

> Well, you can use #instance_method and #bind but ruby will make the
test
> uln% ruby -e 'Array.instance_method(:[]).bind(Hash.new)'
> -e:1:in `bind': bind argument must be an instance of Array
(TypeError)
> from -e:1

this test really kills 'duck typing', and renders the
instance_method/bind trick almost useless :(
Many thanks for the tip though.

-g.

George Moschovitis

1/25/2005 3:39:00 PM

0

> Object methods do not work in vacuum, they usually use instance
> variables to reffer to the object's state, possibly changing it
> (remember encapsulation ;-)?). How would you expect your
> inject_method() deal with it?

I thought Ruby promotes 'duck typing'.

I would like to inject specific methods between specific classes. The
destination class has all the instance variables needed to succesfully
execute the method.

George Moschovitis

1/25/2005 4:05:00 PM

0

> Well, you can use #instance_method and #bind but ruby will make the
test

wow, this is magic!
-g.

Gennady

1/25/2005 4:09:00 PM

0

George Moschovitis wrote:
>>Object methods do not work in vacuum, they usually use instance
>>variables to reffer to the object's state, possibly changing it
>>(remember encapsulation ;-)?). How would you expect your
>>inject_method() deal with it?
>
>
> I thought Ruby promotes 'duck typing'.
>
> I would like to inject specific methods between specific classes. The
> destination class has all the instance variables needed to succesfully
> execute the method.
>

I do not think what you want has anything to do with 'duck typing'.
'duck typing' only means that you can send a message to any object and
if the object has a corresponding method it gets executed regardless of
object type. It does not urge you to abandon the "normal" way of method
definition for a class.

In general, I am very cautious to dynamically adding methods down the
road, as it greatly increases the risk of your system becoming
incomprehensible.

As somebody has indicated, Ruby has other ways to "inject" methods in
some controlled manner, like mixins or inheritance. I would consider
them first before resorting to any sort of "tricks", even if they were
possible (everything is possible in Ruby, you know ;-), one way or another).

Gennady.