What is the purpose of "initialize"

Why do you need initialize and what do you put in the parameter after
initialize(parameter)
?

It’s called a constructor in other languages. it can be used to
initialize
the objects state. The arguments can give variance to the objects
properties
which can alter it’s behavior from the other instances initialized off
the
same class template.

https://secure.wikimedia.org/wikipedia/en/wiki/Constructor_(object-oriented_programming)

~

the initialize method is your constructor it is called when you create a
new
object automatically. It doesn’t have to take a parameter at all. If it
does
then you can set that to an instance variable to use in that class. If
you
didn’t understand much of that you might want to pick up a book on
object
oriented programming it explains in much more detail.


Buddy Lindsey
http://www.buddylindsey.com
http://www.twitter.com/buddylindsey

Well, it is called by default when one instantiates an object from a
class.
Furthermore, a class need not require a parameter upon instantiation. As
in,
object = MyClass.new will set up an object with default values you have
chosen in the ‘initialize’ method. Without ‘initialize’, you inherit the
base constructor from which all all classes are derived.

I should have said “from which all objects are derived”…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs