Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the initialize method in Ruby

I'm trying to wrap my head around the purpose of using the initialize method. In Hartl's tutorial, he uses the example:

def initialize(attributes = {})    @name = attributes[:name]    @email = attributes[:email] end 

Is initialize setting the instance variables @name and @email to the attributes, and, if so, why do we have the argument attributes = {}?

like image 739
thank_you Avatar asked Nov 04 '12 07:11

thank_you


People also ask

How do you initialize a variable in Ruby?

Ruby Class VariablesClass variables begin with @@ and must be initialized before they can be used in method definitions. Referencing an uninitialized class variable produces an error. Class variables are shared among descendants of the class or module in which the class variables are defined.

Does a Ruby class need an initialize method?

The initialize method is useful when we want to initialize some class variables at the time of object creation. The initialize method is part of the object-creation process in Ruby and it allows us to set the initial values for an object.

What is initialization method?

Initialization is the process of locating and using the defined values for variable data that is used by a computer program. For example, an operating system or application program is installed with default or user-specified values that determine certain aspects of how the system or program is to function.

How do you initialize a constructor in Ruby?

To create an Object in Ruby we can use the new keyword with the name of the class like ConstructorExample. new and once we use this command it will internally call the method to initialize. See the below example for use of constructor in Ruby.


1 Answers

Ruby uses the initialize method as an object's constructor. It is part of the Ruby language, not specific to the Rails framework. It is invoked when you instanstiate a new object such as:

@person = Person.new 

Calling the new class level method on a Class allocates a type of that class, and then invokes the object's initialize method:

http://www.ruby-doc.org/core-1.9.3/Class.html#method-i-new

All objects have a default initialize method which accepts no parameters (you don't need to write one - you get it automagically). If you want your object to do something different in the initialize method, you need to define your own version of it.

In your example, you are passing a hash to the initialize method which can be used to set the default value of @name and @email.

You use this such as:

@person = Person.new({name: 'John Appleseed', email: '[email protected]'}) 

The reason the initializer has a default value for attributes (attributes = {} sets the default value to an ampty hash - {}) is so that you can also call it without having to pass an argument. If you dont' specify an argument, then attributes will be an empty hash, and thus both @name and @email will be nil values as no value exists for those keys (:name and :email).

like image 66
Michael Shimmins Avatar answered Sep 23 '22 06:09

Michael Shimmins