When we wish to initialize some class variables at the moment of object creation, we can utilize the initialize method. The initialize function is part of Ruby's object-creation process, and it allows us to set an object's initial properties. Here are some things to think about when it comes to Initialize: We have the option of defining a default parameter.
The first crucial thing to remember is that in Ruby, the method initialize is a special method with a special meaning: When you call a class's new method, such as Person.new, the class creates a new instance of itself. It will then call the function initialize on the new object internally.
Before they may be utilized in method declarations, class variables begin with @@ and must be initialized. An error occurs when a class variable is referenced that has not been initialized. The descendants of the class or module in which the class variables are specified share the class variables.
An initialize method should always be defined in a class or module. It should always have one parameter and it should always return a value. This value can be whatever you want, but it usually returns nil as you don't want to do anything else before your methods are run.
An example of an initialize method would look like this: def my_table_class(name)