On Tue, Oct 22, 2013 at 3:30 PM, Raja gopalan [email protected]
I know of these methods. But My question, Is there any way it gives us
advantages than static keyword which Java uses to define class methods?
Actually the bigger difference is in using class level methods. In
Java you just invoke them from an instance method but in Ruby you
either need to do “self.class.foo” or “MyClass.foo” when invoking
For an example, creating a new object doesn’t require a static method
here as in Java(public static void main) because our Class name itself
is object so new method is readily available in class Class.
I don’t follow that reasoning. We don’t need “main” in Ruby because it
was decided that the whole body of the script is the main function (as
usual with many scripting languages). That has nothing to do with
creating objects or having method Class#new.
The fact that Class#new exists only avoids a special keyword “new”
which Java needs because in Java the class name does not evaluate to
an object. (You need “MyClass.class” for that.)
likewise, Is there any advantages of defining class methods instead of
I don’t think there are advantages to either way: both can be used to
define class level logic. IMHO Ruby is more consistent but that is a
consequence of the fact that classes are regular objects like all
others. This means you can pass them around and do things with them
that you can’t for example do in Java. OTOH Java also has reflection
which allows access to type information at runtime.