In the previous post about Class I’ve described the fact that it’s instance and now it’s time to describe how we define new functionality for such instances and instances which they could produce. Example:

class Person

def initialize(first_name, last_name)

@first_name = first_name

@last_name = last_name

end



def full_name

"#{@first_name} #{@last_name}"

end

end Person.instance_methods(false) # => [:full_name] person = Person.new("Bob", "Martin")

person.full_name # => Bob Martin

As we can see there is a definition of Class instance Person which describe functionality of instances it could produce. Method new is part of Class implementation which is responsible for creating new instance and after such instance is created the private method initialize will be invoked on it. So, both methods initialize and full_name are called instance methods simply because they are available for instances produced by Person , but in the same way method new is instance method of Person instance produced by Class , such methods, available for Class instances we call simply as “class methods”.

So, if we need more functionality for instances produced by Person we could define it in place of creation Person instance, but how we could add some functionality to Person instance itself which is produced by Class instance? Do we need to define it in place of creation Class instance? but where is such a place? — The answer is that we don’t need to modify Class instance to add functionality to the single instance produced by it, such as Person , at list because we need to define some functionality only for Person instance not for all instances produced by Class instance. The solution is that every instance has one more instance behind it and it is linked to the initial one and could be used for such purpose:

class Person

def self.info

"Some description"

end

end # or class Person

class << self

def info

"Some description"

end

end

end # or def Person.info

"Some description"

end # or class << Person

def info

"Some description"

end

end Person.singleton_class.instance_methods(false) # => [:info] Person.info # => Some description

So, if we need some “class methods” for our Class instances such as Person instance then we use the second instance behind it which is called as Metaclass or Eigenclass or Singleton.

def Person.new; end Person.new # => nil

As you can see we redefine new method with our own implementation located in Metaclass. The next question could be — if every instance has some instance behind it then how it works when we try it on instances different from Class instances?

bob = Person.new("Bob", "Martin")

ricky = Person.new("Ricky", "Martin") def bob.full_name

"Robert Cecil #{@last_name}"

end # or class << bob

def full_name

"Robert Cecil #{@last_name}"

end

end bob.full_name # => Robert Cecil Martin

ricky.full_name # => Ricky Martin