[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Class vs. Object

James Herdman

2/1/2006 6:44:00 AM

I'm new to Ruby, and relatively new to OOP (1.5 years of Java -- but
nothing too serious. I didn't really bother to try and understand it
deeply).

I'm trying to grok the difference between a class and an object. My
understanding of an object is that it is a thing, and that a class is
the description of said thing's behavior. Is this correct?

Lastly, programmatically, what is the difference between a Ruby object
and class?

Thank you,

James H.

8 Answers

Dan Kohn

2/1/2006 6:51:00 AM

0

Robert Klemme

2/1/2006 8:56:00 AM

0

James Herdman wrote:
> I'm new to Ruby, and relatively new to OOP (1.5 years of Java -- but
> nothing too serious. I didn't really bother to try and understand it
> deeply).
>
> I'm trying to grok the difference between a class and an object. My
> understanding of an object is that it is a thing, and that a class is
> the description of said thing's behavior. Is this correct?

Yes.

> Lastly, programmatically, what is the difference between a Ruby object
> and class?

A Ruby class is an object at the same time. It's an instance of class
Class. If this sounds wired it's the usual feeling in the beginning.
Once you get more used to it you'll see how powerful this is.

>> String.class.ancestors
=> [Class, Module, Object, Kernel]
>> "".class.ancestors
=> [String, Enumerable, Comparable, Object, Kernel]
>> String.kind_of? Class
=> true
>> String.kind_of? Object
=> true
>> "".kind_of? Class
=> false
>> "".kind_of? Object
=> true
>> "".kind_of? String
=> true

Kind regards

robert

Mark Woodward

2/1/2006 9:49:00 AM

0

On Wed, 1 Feb 2006 01:43:40 -0500
James Herdman <james.herdman@gmail.com> wrote:

....
> I'm trying to grok the difference between a class and an object. My
> understanding of an object is that it is a thing, and that a class is
> the description of said thing's behavior. Is this correct?
....

Hi James,

A class is a 'blueprint' for objects(s). Just as you could use a blueprint of a house to build 10,100,1000 houses, so too you can use a class blueprint to create 10,100,1000 similar objects. So an object is an actual instance of a class.

# 1 class
class Person
def initialize(name)
@name = name
end
def name
@name
end
end

# 2 objects (instances of the class)
fred = Person.new("wilma")
barney = Person.new("betty")

# use the objects
puts fred.name
puts barney.name


HTH,

--

Mark

James Herdman

2/2/2006 1:24:00 AM

0

First of all, thanks to everyone who's replied so far. I appreciate
the help and insight.

I think I'm getting the grip of things.

>>> "".kind_of? Object
> => true

This confused me a bit. "" is an instance of String, right? I know
String is an instance of Object, but that doesn't really make "" an
Object... or does it?

Another thing that confused me is this:

String.kind_of? String
=> false

At first I thought that perhaps an object can't be an instance of
itself as it, itself, is defining said object (i.e. it is a Class --
the blueprint). However,

Object.kind_of? Object
=> true

and

Module.kind_of? Module
=> true

What am I missing?

James H

Robert Klemme

2/2/2006 9:34:00 AM

0

James Herdman wrote:
> First of all, thanks to everyone who's replied so far. I appreciate
> the help and insight.
>
> I think I'm getting the grip of things.
>
>>>> "".kind_of? Object
>> => true
>
> This confused me a bit. "" is an instance of String, right? I know
> String is an instance of Object, but that doesn't really make "" an
> Object... or does it?

It does. Note, that String is an instance of Object (because everything
is an Object in Ruby) as well as being a subclass of Object:

>> String.kind_of? Object
=> true
>> String.ancestors.include? Object
=> true

> Another thing that confused me is this:
>
> String.kind_of? String
> => false

The class object String is by no means an instance of itself. It's rather
an instance of class Class.

> At first I thought that perhaps an object can't be an instance of
> itself as it, itself, is defining said object (i.e. it is a Class --
> the blueprint). However,
>
> Object.kind_of? Object
> => true

Everything is an Object in Ruby - even the class Object itself.

> and
>
> Module.kind_of? Module
> => true
>
> What am I missing?

(i)
>> Class.superclass
=> Module
>> Class.ancestors
=> [Class, Module, Object, Kernel]

(ii)
>> Module.class
=> Class

Since Module is the super class of Class (i) and Module itself is a class
(ii) obviously Module is an instance of Class as well as Module. :-)

I guess, by now I have completely confused you. :-) There's a certain
self referentiality in Ruby's class and object model. But this allows for
some very cool features!

robert

James Herdman

2/2/2006 4:04:00 PM

0

Here we go again =) (I fairly sure I've almost got it now)

> (i)
>>> Class.superclass
> => Module
>>> Class.ancestors
> => [Class, Module, Object, Kernel]

Do I understand the Class.ancestors relationship to mean "those classes
which are implemented by the Class class"? (I had to remind myself that
Ruby isn't really single inheritance -- it has mixins and whatnot)

e.g.

String.ancestors
=> [String, Enumerable, Comparable, Object, Kernal]

Do I understand Class.superclass to mean the class *directly*
subclassed to form said class?

Wow that sounds confusing...

Thanks for your patience,

James

Robert Klemme

2/2/2006 4:15:00 PM

0

James Herdman wrote:
> Here we go again =) (I fairly sure I've almost got it now)
>
>> (i)
>>>> Class.superclass
>> => Module
>>>> Class.ancestors
>> => [Class, Module, Object, Kernel]
>
> Do I understand the Class.ancestors relationship to mean "those
> classes which are implemented by the Class class"? (I had to remind
> myself that Ruby isn't really single inheritance -- it has mixins and
> whatnot)

Yes, all superclasses and mixed in modules (see example below).

> e.g.
>
> String.ancestors
> => [String, Enumerable, Comparable, Object, Kernal]
>
> Do I understand Class.superclass to mean the class *directly*
> subclassed to form said class?

Exactly. And ancestors includes also modules:

>> module Mix;end
=> nil
>> class Base;end
=> nil
>> class Sub < Base
>> include Mix
>> end
=> Sub
>> Sub.ancestors
=> [Sub, Mix, Base, Object, Kernel]
>> Sub.superclass
=> Base


Kind regards

robert

Gary Wright

2/2/2006 4:34:00 PM

0


On Feb 2, 2006, at 11:08 AM, James Herdman wrote:
>> (i)
>>>> Class.superclass
>> => Module
>>>> Class.ancestors
>> => [Class, Module, Object, Kernel]
>
> Do I understand the Class.ancestors relationship to mean "those
> classes which are implemented by the Class class"? (I had to remind
> myself that Ruby isn't really single inheritance -- it has mixins
> and whatnot)

No. X.ancestors returns the list of modules that will be searched
when a method is called on an *instance* of X. I say modules to
include both direct instances of Module and indirect instances of
Module via Class (i.e., Class is a subclass of Module
so instances of Class are also indirectly instances of Module).

The ancestors array is constructed from the strict subclass
relationship *and* dynamically modified by the include method
to implement the mixin of modules. Instead of starting with the
self-referential confusion of Class, lets start with some user-
defined classes:

class A; end
A.ancestors => [A, Object, Kernel]
class A
include Comparable
end
A.ancestors => [A, Comparable, Object, Kernel]

class B < A; end
B.ancestors => [B, C, Comparable, Object, Kernel]


Let's look at String now:

String.ancestors => [String, Enumerable, Comparable, Object, Kernel]

s = String.new # create new instance of String
s.object_id # Ruby will search for the method 'object_id' in
# String, Enumerable, Comparable, Object, and Kernel
# In this case it will find the method in Kernel.

s.size # This time the method is found right away in String

Now lets consider Class. Class is a strange beast since it is an
instance of
itself and also the origin class of all other classes.

String.class # Class
Object.class # Class
Class.class # Class (instance of itself!)

Class.ancestors => [Class, Module, Object, Kernel]

Since Class is an instance of itself, an attempt to call

Class.x

will search for a definition of x in Class, Module, Object, and Kernel.
For example

Class.superclass # Module

will be found in Class itself. Now consider

String.superclass # Object

In this case String is an *instance* of Class (String.class is Class)
so Ruby searches for the superclass method in the modules listed in
Class.ancestors.



Gary Wright