Ruby Syntax: 'initialize' versus 'init'

  • Thread starter Clint Checketts
  • Start date
C

Clint Checketts

------=_Part_8223_20099861.1139017797517
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

While programming with Ruby, I've grown to love just how clear and concise
the language is. (You all already know this of course).

So, why is the object constructor so long? I mean 'initialize' could easily
be replaced with 'init' or even just plain 'new'.

In support of this idea, I've noticed that to_string() is written to_s. The
same is with arrays, integers, and floats.

Even in the Pickaxe book it points out that the spelling of 'initialize' as
being a gotcha. 'Init' seems simpler, to the point, and harder to mispell.

Am I missing any major reasoning behind using the term 'initialize'?
(Besides the fact that is is the status quo)

-Clint

------=_Part_8223_20099861.1139017797517--
 
J

James Britt

Clint said:
While programming with Ruby, I've grown to love just how clear and concise
the language is. (You all already know this of course).

So, why is the object constructor so long? I mean 'initialize' could easily
be replaced with 'init' or even just plain 'new'.

In support of this idea, I've noticed that to_string() is written to_s. The
same is with arrays, integers, and floats.

I suspect people call to_* more often than they call initialize.

But, poor speller that I am, one of the first gvim macros I wrote to
assist my Ruby hacking was a mapping that turned

defi

into

def initialize( )

end

So it became a non-issue for me.

(The other day I shoulder-surfed as someone coded Ruby using Notepad,
and it was so painful to watch even my hair hurt.)
 
G

Gerald Murray

There is a difference in wrapping to interface to C
new() would usually call rb_obj_call_init(...)
and return the class instance.
initialize() usually would set up data.

Ruby is specially designed to be friendly to use and read.
unix commands, by contrast, would be considered unfriendly,
but quicker to type. 'initialize' is within that friendly spirit.

Gerald
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Ruby Syntax: 'initialize' versus 'init'"

|Am I missing any major reasoning behind using the term 'initialize'?
|(Besides the fact that is is the status quo)

It can be very critical when the name of initializing method conflicts
with others, so that I chose "initialize" to avoid potential problems.
Besides that, the name was derived from T language (Scheme dialect).

matz.
 
Y

Yukihiro Matsumoto

Hi,

Interesting you mention T, one question I always wanted to ask is:
Did the T object system influence the design of the Ruby object system?
(Or do you wish it did? ;))

No. I've heard it was pretty interesting but I didn't have any chance
to get the detail more than a small article, which mentioned an
intializer named "initialize".

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top