Forum: Ruby Class vs. Object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
90ebe8da17aabd36cc30d9f96a530e6f?d=identicon&s=25 James Herdman (Guest)
on 2006-02-02 00:14
(Received via mailing list)
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.
5730f209b34b8474639e0c2020f54513?d=identicon&s=25 Dan Kohn (Guest)
on 2006-02-02 00:14
(Received via mailing list)
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-02 00:20
(Received via mailing list)
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
366676a5e96d5fe9087db8a57167d411?d=identicon&s=25 Mark Woodward (Guest)
on 2006-02-02 00:23
(Received via mailing list)
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
90ebe8da17aabd36cc30d9f96a530e6f?d=identicon&s=25 James Herdman (Guest)
on 2006-02-02 02:31
(Received via mailing list)
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-02 05:44
(Received via mailing list)
On Feb 1, 2006, at 8:28 PM, 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
>

#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)
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-02 10:41
(Received via mailing list)
James Herdman 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. :-)

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
90ebe8da17aabd36cc30d9f96a530e6f?d=identicon&s=25 James Herdman (Guest)
on 2006-02-02 17:09
(Received via mailing list)
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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-02 17:19
(Received via mailing list)
James Herdman 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
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2006-02-02 17:34
(Received via mailing list)
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
This topic is locked and can not be replied to.