[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

About class methods

Hank Gong

12/8/2005 6:30:00 AM

Hi! When I read the Ruby manual, I noticed that for class Array, there are
class methods and instance methods.

Class methods:
[] and new

instance methods:
too many (not list here)

My question is: what's this class methods? I remember in C++, all methods
should be instance level, right?
Thanks!

Yours, Hank
58 Answers

Logan Capaldo

12/8/2005 6:39:00 AM

0


On Dec 8, 2005, at 1:30 AM, Hank Gong wrote:

> Hi! When I read the Ruby manual, I noticed that for class Array,
> there are
> class methods and instance methods.
>
> Class methods:
> [] and new
>
> instance methods:
> too many (not list here)
>
> My question is: what's this class methods? I remember in C++, all
> methods
> should be instance level, right?
> Thanks!
>
> Yours, Hank

class methods are (more or less) the same thing as static methods in C
++.

eg.

class A
{
public:
static int aClassMethod { return 42; }
int anInstanceMethod { return 7; }
};

A::aClassMethod( ); //=> returns 42
A x;
x.anInstanceMethod( ); //=> returns 7



james_b

12/8/2005 6:52:00 AM

0

Hank Gong wrote:
> Hi! When I read the Ruby manual, I noticed that for class Array, there are
> class methods and instance methods.
>
> Class methods:
> [] and new
>
> instance methods:
> too many (not list here)
>
> My question is: what's this class methods? I remember in C++, all methods
> should be instance level, right?


p Array.class # Class
p Array.instance_of? Class # true


Array is an instance of Class, so those methods are, in a way, instance
methods.


No?


They're not eigenmethods are they?


:)



James
--

http://www.ru... - Ruby Help & Documentation
http://www.artima.c... - Ruby Code & Style: Writers wanted
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys
http://www.30seco... - Building Better Tools


Trans

12/8/2005 6:56:00 AM

0

In Ruby a class is an object too. So it can have it's own methods just
like any other object. There methods are different from a class
instance methods, which are the ones that define the nature of an
object instantiated from the class. So lets say we have a class:

class MyClass
end

Then we can treat is like any other object and send messages to it.

MyClass.object_id #=> -606921464

That's what is called a *class method*. Also, you may have noticed that
you can define a method tailored specifically for any object. For
instance:

a = "hi"
def a.up
self.upcase
end
a.up #=> "HI"

Class methods are actually the same thing. You can create one of these
tailor-made methods for a class likewise:

def MyClass.upname
name.upcase
end
MyClass.upname #=> "MYCLASS"

HTH,
T.

(Hey all, I didn't use the term adhoc! See I can take a hin... I mean a
brick to the head ;-)

Trans

12/8/2005 7:01:00 AM

0

> They're not eigenmethods are they?
>
> :)

Actually, they are ad hoc. ;-)

Trans

12/8/2005 2:31:00 PM

0

Sorry Hank, if you are unware of what were talking about in these last
two posts. We're refering to a very recent thread that discussed what
to call these type of methods. Presently they are known a "singleton
methods", but becuase of the clash with what is known in OOP generally
as the *singleton pattern*, there is on-going discussion on finding a
better term. Recent alternative suggestions are "eigen" and "adhoc".

T.

Hank Gong

12/8/2005 6:30:00 PM

0

I carefully read two articles about classmethods and singleton concept.
Now for me, the concept of classmethods seems not difficult for me now. It's
just singleton methods for a class because class is also a object.
However, the concept of singleton still not clear for me, also how to use
them is also not making sense for me.

Yours, Hank


On 12/8/05, Trans <transfire@gmail.com> wrote:
>
> Sorry Hank, if you are unware of what were talking about in these last
> two posts. We're refering to a very recent thread that discussed what
> to call these type of methods. Presently they are known a "singleton
> methods", but becuase of the clash with what is known in OOP generally
> as the *singleton pattern*, there is on-going discussion on finding a
> better term. Recent alternative suggestions are "eigen" and "adhoc".
>
> T.
>
>
>

dblack

12/8/2005 7:01:00 PM

0

Trans

12/8/2005 8:02:00 PM

0


dblack@wobblini.net wrote:
> P.S. Aside to Tom S.: I agree it's ad hoc in the sense of being for a
> specific purpose, but I'm not sure I agree that it's ad hoc where the
> "hoc" (actually the "hic", I guess, in the nominative case :-) is the
> object itself. One then runs into questions like: are instance
> variables "ad hoc" variables? etc.

Interesting. Perhaps we've met halfway then. 'Ad hoc' is an excellent
discriptive term, but in trying it on for size some more, so to speak,
I'm not as certain that it makes a good *techncal* term, not because of
negative conotations, but because it may be too generic, much like
'meta'. Which I think it is what you're pointing out here. Of course,
this might be a problem with any term that doesn't already have a
fitting techincal meaning.

T.

jonathan

12/8/2005 8:45:00 PM

0

transfire wrote:
> dblack@wobblini.net wrote:
>> P.S. Aside to Tom S.: I agree it's ad hoc in the sense of being for a
>> specific purpose, but I'm not sure I agree that it's ad hoc where the
>> "hoc" (actually the "hic", I guess, in the nominative case :-) is the
>> object itself. One then runs into questions like: are instance
>> variables "ad hoc" variables? etc.
>
> Interesting. Perhaps we've met halfway then. 'Ad hoc' is an excellent
> discriptive term, but in trying it on for size some more, so to speak,
> I'm not as certain that it makes a good *techncal* term, not because of
> negative conotations, but because it may be too generic, much like
> 'meta'. Which I think it is what you're pointing out here. Of course,
> this might be a problem with any term that doesn't already have a
> fitting techincal meaning.
>
> T.

What did you guys think about using simpleton to refer to these
methods/classes? That way there can be differentiation between a class
which was made single by 'include singleton' (that is, has one single
instance somewhere) and a class which is made single (or simple) by
having only class methods and class data. The one which is an instance
of the design pattern could be called 'singleton' (since it isn't the
one in question anyway) and the other could be called 'simpleton'.

--J

--
Posted via http://www.ruby-....


Gary Wright

12/8/2005 10:00:00 PM

0


On Dec 8, 2005, at 3:45 PM, jonathan <zjll9@imail.etsu.edu>
<zjll9@imail.etsu.edu> <zjll9@imail.etsu.edu> wrote:
> What did you guys think about using simpleton to refer to these
> methods/classes?

I'd vote no on that name regardless. If you think 'ad hoc' has
negative connotations...

> That way there can be differentiation between a class
> which was made single by 'include singleton' (that is, has one single
> instance somewhere) and a class which is made single (or simple) by
> having only class methods and class data.

Are you confusing a class that has no instances (yet) with the class
returned by the expression (class <<obj; self; end) ?

I don't see anything particularly special or interesting about a class
with no instances (I *think* that is what you mean by a class
with 'only class methods and class data'). Certainly that isn't what
all the hubbub has been about regarding 'meta/eigen/singleton/shadow'.


Gary Wright