Class vs. Object

J

James Herdman

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

Robert Klemme

James said:
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
 
M

Mark Woodward

On Wed, 1 Feb 2006 01:43:40 -0500

....
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,
 
J

James Herdman

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.

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
 
R

Robert Klemme

James said:
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.


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:
=> 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, Module, Object, Kernel]

(ii)=> 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
 
J

James Herdman

Here we go again =) (I fairly sure I've almost got it now)
(i)=> [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
 
R

Robert Klemme

James said:
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
 
G

gwtmp01

(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
 

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
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top