Object constructor should be renamed


(Mark A. Kolesar) #1

For consistency and conciseness the object constructor should be renamed from initialize to simply new.

Consider:

class Foo
def initialize(a)
@a = a
end
end

becomes:

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!


(Austin Ziegler) #2

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

···

On Sun, Aug 19, 2018 at 1:59 PM Mark A. Kolesar <mark@ownture.com> wrote:

For consistency and conciseness the object constructor should be renamed
from initialize to simply new.

Consider:

class Foo
     def initialize(a)
         @a = a
     end
end

becomes:

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!

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue


(Dmitriy Non) #3

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.

···

On 19 Aug 2018, at 20:59, Mark A. Kolesar <mark@ownture.com> wrote:

For consistency and conciseness the object constructor should be renamed from initialize to simply new.

Consider:

class Foo
    def initialize(a)
        @a = a
    end
end

becomes:

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!

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>


(Andy Jones) #4

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.


(Robert K.) #5

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

···

On Mon, Aug 20, 2018 at 9:23 AM Andy Jones <Andy.Jones@jameshall.co.uk> wrote:

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.

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