[Ruby] initialize method

less than 1 minute read

initialize method

  • Method called when creating an object from a class

``


class Food
   def initialize
     puts'Initialize'
   end
end
Food.new
#=> Initialized.

・The initialize method is a special method and cannot be called from outside.

``


food = Food.new
food.initialize
#=> NoMethodError: private method'initialize' call for #<Food:0x007gfasjfasfjas>

Tags: ,

Updated: