nik7
nik7

Reputation: 3058

Ruby Object Model clarification

In Ruby, I can write,

Dog = Class.new

so here, Dog is an Object which is an instance of Class.

Also, I can write

fido = Dog.new

which is possible only if Dog is a Class.

Is Dog here a Class or an Object?

Upvotes: 1

Views: 123

Answers (4)

Boris Stitnicky
Boris Stitnicky

Reputation: 12578

First, install 'y_support' by typing gem install y_support in your command prompt. Then, in irb:

require 'y_support/name_magic'

class Animal
  include NameMagic
end # You have created a new class
Animal.name #=> "Animal" -- the class is named Animal
class Dog < Animal
  def speak; puts "Bow wow!" end
end #=> hereby, you have created a subclass of Animal class
Cat = Class.new( Animal ) do
  def speak; puts "Meow!" end
end #=> this is another way of creating a subclass
Dog.name #=> "Dog" -- this is a class named Dog

And now

Fido = Dog.new      #=> You have created a new Dog instance
Dog.instance_names  #=> [:Fido] -- the instance is named Fido
Stripes = Cat.new   #=> You have created a new Cat instance
Cat.instance_names  #=> [:Stripes] -- the instance is named Cat
Animal.instances.size #=> 2 -- we have 2 animals thus far
Animal.instances.each do |animal| animal.speak end #=> Bow wow! Meow!

Let's create another dog:

Spot = Dog.new      #=> Another Dog instance
Dog.instances.size  #=> 2 -- we now have 2 dogs
Fido.class          #=> Dog -- Fido is an instance of class Dog
Spot.class          #=> Dog -- Spot is also an instance of class Dog
Fido.class.ancestors #=> The output shows you that Fido is also an Animal
Animal.instances.size #=> 3 -- together, we have 3 animals
Animal.instance_names #=> [:Fido, :Stripes, :Spot]
Animal.instance( :Fido ).speak #=> Bow wow!
Animal.instances.each &:speak #=> Bow wow! Meow! Bow wow!

Understood? Remember, in Ruby, never work without NameMagic.

Upvotes: 0

Arup Rakshit
Arup Rakshit

Reputation: 118261

Ask the object itself, to know where they belongs to, like below:

Dog = Class.new
fido = Dog.new
Dog.instance_of? Class #=> true
fido.instance_of? Class #=> false
fido.instance_of? Dog #=> true
Dog.superclass #=> Object
Dog.is_a? Object #=> true
Dog.is_a? Class #=> true

To look into in more detail see the Object model Documentation

Upvotes: 5

sawa
sawa

Reputation: 168071

I think you are making a mistake that some beginners repeatedly do. You are confusing the two meanings of "is":

  • A is an instance of B, and
  • A is a subclass of B.

With your case,

  • Dog is an instance of (but not a subclass of) Class, and
  • Dog is a subclass of (but not an instance of) Object.

So, in different senses, it is a Class and is an Object.

When they say "everything in Ruby is an Object", it does not mean that everything is an instance of Object. It means that everything is an instance of a (reflexive) subclass of Object.

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

Everything in ruby is an Object (except for blocks). And Dog here is also a Class.

Dog = Class.new
fido = Dog.new

So the answer is: both.

Upvotes: 5

Related Questions