Difference in public class methods and instance methods?

What is difference between public class methods and public instance
methods?
I am interested in knowing in detail way.

On Wed, Aug 20, 2008 at 7:36 AM, Sunny B. <
[email protected]> wrote:

What is difference between public class methods and public instance
methods?
I am interested in knowing in detail way.

Class methods are called on the class, whereas instance methods are
called
on instances of the class.

class Foo
def self.class_method
end

def instance_method
end
end

Foo.class_method
Foo.new.instance_method

Find more information here:
http://www.rubycentral.com/pickaxe/tut_classes.html

Brandon


Sessions by Collective Idea: Ruby on Rails training in a vacation
setting
http://sessions.collectiveidea.com

“Public” refers to the accessibility of the methods–public methods can
be called from anyplace. Compare private methods, which are only
accessible from within the module where they are defined (or something
close to that).

Instance methods are called on (and operate on) particular well,
instances of a class. In this code:

x = “boogers”
x.length # -> 7

The .length method on the String class is an instance method. We are
asking this particular instance of String (which we have given the
name ‘x’) how many characters it has.

Class methods are called on the class itself. For example, String.new
is a class method–the class creates a new string and returns it. You
couldn’t say x.new–x is just an instance of string. The only thing
that can create new instances of String is the String class. In other
languages these are often called ‘static’ methods.

For more detail, I recommend David Black’s Ruby For Rails, and/or the
pickaxe book.

HTH,

-Roy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs