Discussion:
Object constructor should be renamed
Dmitriy Non
2018-08-19 19:33:37 UTC
Permalink
It, actually, does not make sense.

`#new` is a method of class `Class` and `#initialize` is a
instance method of any class that is called whenever object is created.

So, first you create object with `MyClass.new` and than you initialize it's state with
`MyClass#initialize`. That makes perfect sense. At least, to me.
For consistency and conciseness the object constructor should be renamed from initialize to simply new.
class Foo
def initialize(a)
@a = a
end
end
class Foo
def new(a)
@a = a
end
end
my_a = Foo.new("test")
Less typing, more consistent. What's not to like?
--
Cheers,
Mark A. Kolesar
Ownture
--
Windows provide a limited view. Come out into the open and embrace freedom!
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Austin Ziegler
2018-08-19 18:53:31 UTC
Permalink
Just about everything, including 20+ years of history in Ruby. You’ll
need much better arguments than “consistency” and concision.

What you’re missing is that new, the class method, is doing more
behind the scenes. What really happens is closer to:

class Foo
def self.new(*args)
object = allocate
object.initialize(*args)
end

def initialize(*args)
# initialize a Foo instance here
end
end

While you almost never see an override of Object.allocate, there’s
nothing that prevents it from happening for your own classes.

See also http://ruby-doc.org/core-2.5.1/Class.html#method-i-allocate.

-a
For consistency and conciseness the object constructor should be renamed
from initialize to simply new.
class Foo
def initialize(a)
@a = a
end
end
class Foo
def new(a)
@a = a
end
end
my_a = Foo.new("test")
Less typing, more consistent. What's not to like?
--
Cheers,
Mark A. Kolesar
Ownture
--
Windows provide a limited view. Come out into the open and embrace freedom!
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
--
Austin Ziegler • ***@gmail.com • ***@halostatue.ca
http://www.halostatue.ca/ • http://twitter.com/halostatue

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby
Andy Jones
2018-08-20 07:23:22 UTC
Permalink
If we had the language to do over again I would prefer "init" since (a) here in the UK we don't spell that word with a Z and (b) I seem incapable of typing it correctly, and when I misspell it, it's sometimes hard to debug. ("Why is the initialise method not being ... Oh.")

But of course we don't have the language to do over again, and renaming it is not a thing we can do.


Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Robert Klemme
2018-08-20 14:25:17 UTC
Permalink
Post by Andy Jones
If we had the language to do over again I would prefer "init" since (a) here in the UK we don't spell that word with a Z and (b) I seem incapable of typing it correctly, and when I misspell it, it's sometimes hard to debug. ("Why is the initialise method not being ... Oh.")
But of course we don't have the language to do over again, and renaming it is not a thing we can do.
Even if renaming was possible, reusing a method name that is used in a
similar context but with different functionality would only cause
confusion as others have pointed out already.

Cheers

robert
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Continue reading on narkive:
Loading...