[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: How to copy a method from one class to another

Gavin Kistner

9/25/2006 4:44:00 PM

From: Sam Kong [mailto:sam.s.kong@gmail.com]
> Now I want C2 to have f method so that I can call C2.new.f.

Select lines 2-4.
Copy.
Move caret inside C2.
Paste.

:)

Ruby's methods are not first-class functions like Lua or JavaScript
have. Other, first-class functions exist (procs/lambdas), but Methods
are not these.

Others have given you good suggestions (modules, inheritance). I think
the most important advice is to describe what you want to do instead of
how you think you might want to accomplish it. :)



Here's one other technique, that very likely is too complicated and
isn't what you wanted. It gives C2 access to the proc defined at the
class level of C1, which it can invoke on itself.

Of course, any methods you try to invoke on 'self' inside that proc must
exist both for the C1 and the C2 instance...in which case, you probably
wanted inheritance or module inclusion anyhow.

class C1
class << self
attr_reader :complex_stuff
end
@complex_stuff = lambda{
puts "My encoded representation is #{self.inspect.reverse}"
}

def initialize( name )
@name = name
end

def do_complex_stuff
self.instance_eval( &self.class.complex_stuff )
end
end

C1.new( 'charlie' ).do_complex_stuff
#=> My encoded representation is >"eilrahc"=eman@ 8194382x0:1C<#

class C2
def do_complex_stuff
self.instance_eval( &C1.complex_stuff )
end
end

C2.new.do_complex_stuff
#=> My encoded representation is >0714382x0:2C<#

5 Answers

Logan Capaldo

9/26/2006 12:47:00 PM

0

On Tue, Sep 26, 2006 at 05:24:48PM +0900, Robert Dober wrote:
> On 9/25/06, Gavin Kistner <gavin.kistner@anark.com> wrote:
> >
> ><SNIP>
> >Ruby's methods are not first-class functions like Lua or JavaScript
> >have. Other, first-class functions exist (procs/lambdas), but Methods
> >are not these.
>
>
> I tend to disagree, I do not know about Lua, but for me Ruby's methods are
> pretty much first class
Sometimes people define first class in terms of the additional syntax
compared to other things you must use.
> <code>
> class A
> def a; puts "#{self} == 42"; end
> end
> class B < A
> define_method :another_a, A.instance_method(:a)
> end
> AA = Class.new A
>
> A.new.a
> A.instance_method(:a).bind(AA.new).call
> B.new.another_a
> </code>
>
> I do not know what can be done more in Lua and JavaScript but would be happy
> to know.
>
> <SNIP>
>
Interesting. Doesn't work outside of inheritence hierarchys though,
where in JavaScript you could. (Lua of course has no object system, save
whatever you want to graft on).
> Cheers
> Robert
> --
> Deux choses sont infinies : l'univers et la b?tise humaine ; en ce qui
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
>
> - Albert Einstein

Trans

9/26/2006 12:55:00 PM

0


Logan Capaldo wrote:
> On Tue, Sep 26, 2006 at 05:24:48PM +0900, Robert Dober wrote:
> > On 9/25/06, Gavin Kistner <gavin.kistner@anark.com> wrote:
> > >
> > ><SNIP>
> > >Ruby's methods are not first-class functions like Lua or JavaScript
> > >have. Other, first-class functions exist (procs/lambdas), but Methods
> > >are not these.
> >
> >
> > I tend to disagree, I do not know about Lua, but for me Ruby's methods are
> > pretty much first class
> Sometimes people define first class in terms of the additional syntax
> compared to other things you must use.
> > <code>
> > class A
> > def a; puts "#{self} == 42"; end
> > end
> > class B < A
> > define_method :another_a, A.instance_method(:a)
> > end
> > AA = Class.new A
> >
> > A.new.a
> > A.instance_method(:a).bind(AA.new).call
> > B.new.another_a
> > </code>

They are Functor, but they are not 1st class

Object.instance_method(:send).object_id ==
Object.instance_method(:send).object_id
=> false

T.

Gary Wright

9/27/2006 9:13:00 PM

0


On Sep 27, 2006, at 4:47 PM, Robert Dober wrote:
> I would define Ruby Methods as first class objects as they meet
> *all* the
> conditions above

If you are talking about instances of the Method class, then yeah, they
are first class objects, but they are really just wrappers that delegate
to the underlying 'method', which I don't think is ever *directly*
accessible.


So the code/data structure that gets created under the hood via
something like:

def a_method; end

and which we call a 'method' isn't really the same thing as an
instance of Method,
which we also call a method. :-)

So I would say that instances of Method are first class objects but
that methods
themselves are not first class objects.

This is similar to the way that instances of Proc are wrappers around
Ruby
blocks, which are also never *directly* accessible. Proc instances
are first
class objects, but blocks aren't.

Another example might be the array of bytes that is associated with
an instance
of String. Strings are first class objects but the data associated
with a string
is not.

At least that is the way I think about these things. I'm sure if I
was up to
speed on ruby internals it would be quite easy to point out the
differences between
method definitions and instances of Method and blocks and instances
of Procs.


Gary Wright




Vincent Fourmond

9/27/2006 9:32:00 PM

0

Robert Dober wrote:
> On 9/27/06, gwtmp01@mac.com <gwtmp01@mac.com> wrote:
>>
>>
>> <SNIP>
>>
>> Another example might be the array of bytes that is associated with
>> an instance
>> of String. Strings are first class objects but the data associated
>> with a string
>> is not.
>
>
> ty Gary you make my point, when we talk about strings, methods objects we
> talk about the interfaces Ruby gives us to them, everything else is out of
> context, implementation if you want.
>
> IOW there is no string or method in Ruby, there is only String, Method,
> UnboundMethod etc, etc.
> we use the words string method to explain concepts to human beings, Ruby is
> not a human being (yet ;).


However, there is quite a difference between String/string and
Method/method : for String, you can only access the string via the
String class. For the methods, you fortunately don't need the Method
class to execute methods...

The Method class is just a thin facility (too thin, IMHO) provided for
simple methods manipulations, whereas String is a full-blown string
manipulation library.

But if you want to copy methods from one place to the other, try
programming some C extensions to do that ;-)...

Cheers !

Vince

Rick DeNatale

9/28/2006 2:15:00 AM

0

On 9/27/06, Robert Dober <robert.dober@gmail.com> wrote:
> On 9/27/06, Vincent Fourmond <vincent.fourmond@9online.fr> wrote:

> > However, there is quite a difference between String/string and
> > Method/method : for String, you can only access the string via the
> > String class.
>
>
> Hmmm that is a point I might have overlooked in the definition
> because as a matter of fact
> 'this string' is a full fledged object (represented by the literal) while
>
> class A
> def a; 42; end
> end
>
> the "text" def a; 42; end
> might not be considered a literal.
> But is the literal requirement strict? Honestly I dunno

Well, I for one think that it is. I made that comment on the
discussion page of the wikipedia article you quoted over a month ago:

http://en.wikipedia.org/wiki/Talk:First-cl...

See the second comment "Is it or Ain't It"

Note that that list of the characteristics of a first class object in
the wikipedia article is preceded by:
"Depending on the language, this can imply:"

Which seems to make the whole list rather subjective anyway.

Of course if you trace the wikipedia stance on what a literal value is
you will find in the referenced article:
"any notation for representing a value within programming language
source code; for example, a string literal"

So I'd say that the source code of the method is another example of a
notation for expressing a value within programming language source
code.
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...