Last Updated: February 25, 2016
·
2.938K
· sweatypitts

Self during Ruby class initialization

When you are defining the initialize method of a Ruby class, should you be calling class or instance methods from that class?

You should be calling instance methods. Once the interpreter begins executing the initialize method, your instance has already been instantiated. Thus, self refers to the instance.

class Foo
  def initialize
    puts self.class
  end
end

Foo.new
#=> Foo