Why initialize?

R

Ralph Mason

One thing I have often wondered is why the ruby constructor isn't called
new?

In my laziness I would rather type new than initialize.

It seems clearer than initialize, and appears to work just fine.
Perhaps both could be supported?

What thoughts?
Ralph



class Foo
def new
puts "Foo.new"
end
def Foo.new
x=super
x.new
x
end

def bar
puts "Foo:bar"
end
end

Foo.new().bar
 
G

Gennady

Ralph said:
One thing I have often wondered is why the ruby constructor isn't called
new?

In my laziness I would rather type new than initialize.

It seems clearer than initialize, and appears to work just fine.
Perhaps both could be supported?

What thoughts?
Ralph



class Foo
def new
puts "Foo.new"
end
def Foo.new
x=super
x.new
x
end
def bar
puts "Foo:bar"
end
end

Foo.new().bar
Search ruby-talk archives at
http://www.ruby-talk.org/ruby/ruby-talk/index.shtml
for many discussions on the issue.

Gennady.
 
R

Robert Klemme

Ralph Mason said:
One thing I have often wondered is why the ruby constructor isn't called
new?

Maybe because there are two things involved in object construction and
different names make this clearer: first an instance is *allocated* and
then it is *initialized*. At the point in time when initialize is run,
the memory is already allocated.

Regards

robert
In my laziness I would rather type new than initialize.

It seems clearer than initialize, and appears to work just fine.
Perhaps both could be supported?

You can do

class Foo
def i(bar, baz)
@bar, @baz = bar, baz
self
end
end

f = Foo.new.i("how's", "that?")

Regards

robert
 
N

nobu.nokada

Hi,

At Thu, 15 Jan 2004 07:49:26 +0900,
Ralph said:
One thing I have often wondered is why the ruby constructor isn't called
new?

Note that classes also are objects. Singleton method new and
instance method initialize have to be separated.
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top