ruby help

S

Sunny Bogawat

Can someone enlighten me about

* Public Instance Methods.
* Public Class Methods.
* What is the root class for all Ruby Classes if there is one.
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]

2008/8/20 Sunny Bogawat said:
Can someone enlighten me about

* Public Instance Methods.
* Public Class Methods.
* What is the root class for all Ruby Classes if there is one.



This is a little vague, but I'll try to help. In Ruby, Object is the root
class, and all classes (even Object) are instances of the Class class. Class
inherits from Module, which inherits from Object.

Object
^
|
Module
^
|
Class

So Object, Module and Class are all Classes, and are also all Objects. Hope
that makes sense.

Public instance methods are defined within a class and can be called on
instances of the class:

class Foo
def talk
'talking'
end
end

f = Foo.new
f.talk #=> 'talking'

Class methods are defined on the class itself (notice the use of self):

class Foo
def self.something
self.name
end
end

Foo.something #=> 'Foo'

'Public' just means that a method can be called from outside the object by
any other piece of code. Private methods can only be called inside an object
by other methods in the same class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top