Class vs. Object


#1

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.


#2

Here’s a good place to start:

http://www.ruby-doc.org/docs/UsersGuide/rg/oothinking.html


#3

James H. 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

#4

On Wed, 1 Feb 2006 01:43:40 -0500
James H. removed_email_address@domain.invalid 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


#5

On Feb 1, 2006, at 8:28 PM, James H. 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

#kind_of? tests if an object is an instance of the given class. “” is
an instance of String, which means its also an instance of Object.

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?

You are right, that doesn’t make it an object. What does make it an
Object is that the class String inherits from the class Object. This
means anywhere you can use an instance of Object, you can use an
instance of String. Therefore, “” is indeed a kind_of Object

Another thing that confused me is this:

String.kind_of? String
=> false

String is not an instance of String, or of any of String’s descendants

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

Just remember, in Ruby, everything is an object. (Everything that can
receive messages anyway.) So of course Object is an Object.

and

Module.kind_of? Module
=> true

Yeah this one is weird. (Well so was the last one) Module is actually
the base class of Class. Modules are sort of like classes, but you
can’t inherit from them. Anyway any class is a kind_of Class and by
extension a kind_of Module. Any module is also a kind_of Module.
Module happens to be an instance of Class. Therefore Module.kind_of?
Module is true.

kind_of could be defined as follows:

a.kind_of? B is true if B.ancestors.include?(a.class)


#6

James H. wrote:

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. :slight_smile:

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

robert

#7

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


#8

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


#9

James H. wrote:

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

#10

On Feb 2, 2006, at 11:08 AM, James H. 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 W.