Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #6438 > unrolled thread

difference between class and object methods

Started bySagy Drucker <sagysrael@gmail.com>
First post2012-01-31 04:29 -0800
Last post2012-02-03 11:51 +0000
Articles 5 — 5 participants

Back to article view | Back to comp.lang.ruby


Contents

  difference between class and object methods Sagy Drucker <sagysrael@gmail.com> - 2012-01-31 04:29 -0800
    Re: difference between class and object methods Robert Klemme <shortcutter@googlemail.com> - 2012-01-31 21:21 +0100
    Re: difference between class and object methods Rodrigo Kochenburger <divoxx@gmail.com> - 2012-02-01 07:41 -0800
    Re: difference between class and object methods Danny Woods <dannywoodz@yahoo.co.uk> - 2012-02-02 21:47 +0000
      Re: difference between class and object methods Alec Ross <alec@arlross.demon.co.uk> - 2012-02-03 11:51 +0000

#6438 — difference between class and object methods

FromSagy Drucker <sagysrael@gmail.com>
Date2012-01-31 04:29 -0800
Subjectdifference between class and object methods
Message-ID<c4ed84a5-78ee-442f-80f5-9996b5ffceb7@o12g2000vbd.googlegroups.com>
hello
am i correct when i compare to java, and say:
object methods in ruby are equivalent object methods in java
and class methods in ruby are equivalent STATIC functions in java?

also,
class variables in ruby are equivalent to static variables in java?

thanks...
sagy... ( a very new ruby programmer )

[toc] | [next] | [standalone]


#6439

FromRobert Klemme <shortcutter@googlemail.com>
Date2012-01-31 21:21 +0100
Message-ID<9or0qdFjmoU1@mid.individual.net>
In reply to#6438
On 31.01.2012 13:29, Sagy Drucker wrote:
> am i correct when i compare to java, and say:
> object methods in ruby are equivalent object methods in java
> and class methods in ruby are equivalent STATIC functions in java?
>
> also,
> class variables in ruby are equivalent to static variables in java?

Yep, that's mostly correct.  But you should rather not bother to use 
class variables (those prefixed with @@) but instead should only use 
instance variables of a class, e.g.

class X
   def self.a_class_method
     @@do_not_use_class_variables = 1
     @but_use_class_instance_variables = 2
   end
end

Reason is that class variables have weird scoping rules which can 
produce strange effects.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#6441

FromRodrigo Kochenburger <divoxx@gmail.com>
Date2012-02-01 07:41 -0800
Message-ID<2839975.745.1328110887804.JavaMail.geo-discussion-forums@yqnd19>
In reply to#6438
I'm not sure I'm entirely sure about this but I think comparing class level methods/variables to static methods is wrong. Please, correct me if I'm wrong.

Class methods are similar to static functions in java in terms of both being methods defined in what, conceptually, is a class. It is not really a static method because the interpreter will still do a dynamic dispatching of the method because ruby classes are still objects, instances of the class Class.

Class variables are variables that lives on the Class object and it is also accessible from all the instances and subclasses. If you change the value of the variable in a subclass, it will change the original value, affecting all classes and objects associated.

There is also the concept of class instance variable, which is a instance variable that resides in the singleton instance of the class. 

More about this here: http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/

[toc] | [prev] | [next] | [standalone]


#6443

FromDanny Woods <dannywoodz@yahoo.co.uk>
Date2012-02-02 21:47 +0000
Message-ID<m3pqdwaowx.fsf@mirror.lair>
In reply to#6438
Sagy Drucker <sagysrael@gmail.com> writes:

> hello
> am i correct when i compare to java, and say:
> object methods in ruby are equivalent object methods in java
> and class methods in ruby are equivalent STATIC functions in java?

Instance methods in Ruby can be considered semantically equivalent to
instance methods in Java.

Ruby's class methods are quite different.  In Ruby, classes are
themselves objects that are available at runtime (yes, it's somewhat
circular), and so can have their own methods and variables.  Classes are
instances of the class Class (e.g. String.class == Class; this ties up at
the end, where Class.class == Class).

Java's static methods and variables are not class methods in the same
sense.  In Java, you cannot refer to 'super' in a static method, as
there is no 'this' (q.v. self, the current object; classes aren't objects in
Java.  In a Ruby class method, 'self', refers to the class object).
Since there is no this/self, static methods in Java cannot be overridden in
subclasses (although they can be replaced by a static method with the
same signature).  Being associated with a real object (the class), Ruby
class methods can be thought of as instance methods; they're just
methods on the class, not an individual instance of it.

Hope that helps.

Cheers,
Danny

[toc] | [prev] | [next] | [standalone]


#6445

FromAlec Ross <alec@arlross.demon.co.uk>
Date2012-02-03 11:51 +0000
Message-ID<wXeF9OEKp8KPFwxY@arlross.demon.co.uk>
In reply to#6443
In message <m3pqdwaowx.fsf@mirror.lair>, Danny Woods 
<dannywoodz@yahoo.co.uk> writes
>Sagy Drucker <sagysrael@gmail.com> writes:
>
>> hello
>> am i correct when i compare to java, and say:
>> object methods in ruby are equivalent object methods in java
>> and class methods in ruby are equivalent STATIC functions in java?
>
>Instance methods in Ruby can be considered semantically equivalent to
>instance methods in Java.
>
>Ruby's class methods are quite different.  In Ruby, classes are
>themselves objects that are available at runtime (yes, it's somewhat
>circular), and so can have their own methods and variables.  Classes are
>instances of the class Class (e.g. String.class == Class; this ties up at
>the end, where Class.class == Class).
>
>Java's static methods and variables are not class methods in the same
>sense.

But of course in there is an established terminology for "class methods" 
and "static methods" (see, e.g.:

  http://en.wikipedia.org/wiki/Class_method#Class_methods

), where, where, as it states there that "class methods are synonymous 
with static methods" in Java and C++.  And indeed also in C#, see. e.g.:

  http://msdn.microsoft.com/en-us/library/aa645766%28v=vs.71%29.aspx ,

See also the comments on that Wikipedia entry in respect of Ruby in this 
regard.

>In Java, you cannot refer to 'super' in a static method, as
>there is no 'this' (q.v. self, the current object; classes aren't objects in
>Java.  In a Ruby class method, 'self', refers to the class object).
>Since there is no this/self, static methods in Java cannot be overridden in
>subclasses (although they can be replaced by a static method with the
>same signature).  Being associated with a real object (the class), Ruby
>class methods can be thought of as instance methods; they're just
>methods on the class, not an individual instance of it.
>
>Hope that helps.
>
>Cheers,
>Danny


HTH,

Alec
-- 
Alec Ross

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web