Class methods and static keyword

hi

For an example, if we declare a class, this class name has already been
an object of another class named Class. Now, we define the public class
methods using self.methodname. In jave we can use static keyword to
define this class method. But Ruby is defining the class method in a
different way than this.Now I would like to know Is there any advantages
in ruby way? Or only different styles?

No doubt there are differences in the implementation, but a keyword is
just a keyword. Ruby is more dynamic in the way that class methods can
be defined, of course:

class << self
def classmethod

def MyClass.classmethod

def self.classmethod

define_singleton_method :classmethod

There are probably some more I don’t know about.

I know of these methods. But My question was, Is there any way it gives
us advantages than static keyword which Java uses to define class
methods?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. So
likewise, Is there any advantages of defining class methods in Ruby way
compared to Java’s style?

Java is a compiled statically typed language that was put forward as a
replacement for C++. To achieve that (like C++) there are a variety of
keywords that the programmer can use (such as const, final, abstract,
static) that helps the compiler optimise the resulting program for both
speed and size. Compiler design has pushed Java to be easier to
optimise,
such as homogenous arrays.

Ruby is not statically typed and was never sold as being as fast as C++.
Design goals for Ruby are for a dynamic language that is easy to express
solutions in taking it’s cues from Smalltalk and Lisp. Behind the scenes
the Ruby program does a great deal of work to make the program you have
written run regardless the amount of work that needs to be done to
achieve
this. Speed has never been a primary concern.

The best way to learn Ruby is to learn Ruby.

Ruby is not Java, Java is not Ruby. More than the similarities you will
learn that there are different ways to do things.

On Tue, Oct 22, 2013 at 3:30 PM, Raja gopalan [email protected]
wrote:

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

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

So
likewise, Is there any advantages of defining class methods instead of
static keyword?

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.

Kind regards

robert

here is no static keyword and it would not make sense for ruby

you need to understand that class methods in ruby does work different
than static methods in Java

hi Peter H.

Java uses both Compiler and Interpreter, Compiler converts source code
into Byte Code subsequently Interpreter runs this Byte Code as Native
code. So at the end both Java and Ruby runs by Interpreter.

hi Hans M.

<>

I know of this, But Ruby class methods and static methods doing the same
work.That’s what My question was, Is there any advantages in the way of
Ruby.

Hi Robert K.

You cleared many things here.One among them was,
<<This means you can pass them around and do things with them
that you can’t for example do in Java. >>
This is the logic I haven’t thought of that.