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

From: Sam K. [mailto:[email protected]]

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.

:slight_smile:

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. :slight_smile:

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<#

On 9/25/06, Gavin K. [email protected] wrote:

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

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

I do not know what can be done more in Lua and JavaScript but would be
happy
to know.

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

Logan C. wrote:

pretty much first class

A.new.a
A.instance_method(:a).bind(AA.new).call
B.new.another_a

They are Functor, but they are not 1st class

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

T.

On Tue, Sep 26, 2006 at 05:24:48PM +0900, Robert D. wrote:

On 9/25/06, Gavin K. [email protected] wrote:

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.
A.instance_method(:a).bind(AA.new).call
B.new.another_a

I do not know what can be done more in Lua and JavaScript but would be happy
to know.

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).

On 9/26/06, Trans [email protected] wrote:

class B < A

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

Aha, I do not have the faintest idea what 1st class means - now because
I
follow suit to your knowledge - would you care to elaborate a little
more?
I guess that the unidentity you state above might come from some wrapper
object dynamically allocated when getting the “object”.

Note to Logan: not being able to bind the method to an object outside of
the
inheritance tree stuck me at first too, but I think it is a good thing.

OTOH it is somehow viaolating the idea of “MetaDuckTyping” (surely
McCarthy
did not say that) meta applies to DuckTyping it is not the type of a
MetaDuck we are discussing :wink:

Cheers
Robert

T.


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
> > > > So I thaught somebody might come up with a definition, but no :(

Wikipedia tells us

first class objects are

I would define Ruby Methods as first class objects as they meet all
the
conditions above

Object.instance_method(:send) == Object.instance_method(:send) ==>
true,
Trans what was that object_id thing about?</Shaking
forefinger>

I hope you all agree that strings are first class objects and of course
“a”==“a” ==> true
but
“a”.object_id == “a”.object_id ==> false.

Nobody gonna talk me out of it anymore, Ruby’s methods are 1st class
(not
buisness class or ecconomie class, no Sir) First Class :slight_smile:

Cheers
Robert

Any argument would lead back to “is object_id identity aequivalent to
object
identity”, hopefully we can spare this.

R.

On Sep 27, 2006, at 4:47 PM, Robert D. 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. :slight_smile:

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 W.

On 9/27/06, [email protected] [email protected] wrote:

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 ;).

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.

Which would be interesting of course but does not at all conflict with a
statement made on the level of the services the language is giving us.
It does not matter how they are implemented as long as the
implementation is
correct corresponding to the defintion of the language, and the
definition
of first class seems to be satisfied by the implementation of Method.

Gary W.

Robert

Robert D. wrote:

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

On 9/27/06, Vincent F. [email protected] wrote:

with a string
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.

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

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

I agree completly, I too would like more service but that does not
have
any influence on “first class”.
Immagine a class where you delete all inherited methods, an object of
that
class would still be “first class”
(bare the literal requirement, or we could strip all methods from Hash
e.g.
and we would have literals)
Hmmm it seems that I am sticking out my neck here !!!

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 ;-)…

No Ruby can to that as I have shown above, and you said the C word
nasty
boy :wink:

Cheers !

    Vince

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

On 9/28/06, Rick DeNatale [email protected] wrote:

On 9/27/06, Robert D. [email protected] wrote:

On 9/27/06, Vincent F. [email protected] wrote:

Talk:First-class citizen - Wikipedia

See the second comment “Is it or Ain’t It”

Good points Rick

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.

Yup

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.

That is exactly where I got confused - and I am still - again your
point is
valid
As conclusion I still maintain my claim, and I will try to be as formal
as
possible
The object “Method” as a representation of methods in Ruby is first
class in the sense that they fulfill most of the “requirements”
and allow runtime manipulation which seems the main pratical issue, c.f.
OP’s question and my answer :wink:

Thx for your time
Robert

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/


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

On 9/27/06, Robert D. [email protected] wrote:

On 9/27/06, Vincent F. [email protected] wrote:

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:

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.denhaven2.com/