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