[ruby-core:32967] Re: Behavior of initialize in 1.9

From: Marc-Andre Lafortune <ruby-core-mailing-list@...>
Date: 2010-10-30 03:06:11 UTC
List: ruby-core #32967
Hi,

On Thu, Oct 28, 2010 at 3:25 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
> why is:
>
> .new(42)
>
> OK to do, but not:
>
> .new.y(42)
>
> ?

Sorry for now answering earlier. I have been very occupied for some
time and I'm just getting back to Ruby.

I'd like to point out that {Basic}Object#initialize is quite special.
It is the only built-in method which is "meant" to be overridden with
different set of arguments; it's also never called directly. Ruby is
full of cases where it chooses to be pragmatic at the expense of
"purity" (can't find a better adjective, sorry). One can think of
"super" vs "super()" or "super(&nil)", for example.

`initialize` accepting any number of arguments would have been such a choice.

> We have argument checking for a reason, we shouldn't throw it on the floor. If you want to loosen it, then monkeypatch BasicObject#initialize. That's what monkeypatching is for.

I won't claim to know for sure what monkeypatching is for, but I don't
see how that would be a good solution here. Anyways, the idea was to
provide a dirt cheap way of making constructor easily expandable. If
someone needs it, there are other solutions and for the bulk of the
classes, I imagine people will continue not bothering to call `super`
at all.

In This Thread