jqockw
Last Updated: February 25, 2016
·
1.08K
· sweatypitts
Bf88e319077e4746a6efb4762f6d4b74

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
Say Thanks
Respond
Awesome Job

E4857be2 008e 11e8 8271 f03109811178
Software Engineer
·
San Francisco Bay Area (San Carlos)
·
Full Time