The initialize Method

initialize is a special method that runs when an object is created. It’s similar to constructors in other languages.

Example:

class Dog
  def initialize(name)
    @name = name
  end
end
dog = Dog.new("Max")

@name is an instance variable accessible across methods in the object.

← PrevNext →