On Tue, Feb 21, 2012 at 11:44 AM, James French
<James.French / naturalmotion.com> wrote:
>
> From: peter.vandenabeele.be / gmail.com
> [mailto:peter.vandenabeele.be / gmail.com] On Behalf Of Peter Vandenabeele
> Sent: 21 February 2012 10:01
> To: ruby-talk ML
> Subject: Re: Difference between 1.9.2 and 1.9.3
>
>
>
> On Tue, Feb 21, 2012 at 9:40 AM, Bartosz Dziewoski <matma.rex / gmail.com>
> wrote:


> Just write `super()` when calling the higher up initializer.

But do it in the child class not in the module!

> Calling super() does fix this trivial example but I also happen to include
> the module from a class that inherits from another class before Object, so
> it breaks that. What we need to figure out is how does one write
> initialize() in a module such that it seamlessly integrates into any
> inheritance chain in 1.9.3. It doesn seem possible (at least without some
> hacking) in 1.9.3. Maybe it is actually a bug somewhere.
>
>
>
> I will probably hack round it with
>
>  def initialize(*args, &block)
>  if self.class.superclass === Object
>  super()
>  else
>  super
>  end
>  end
>
> Is there a better way?

IMHO the quoted 1.9.2 version is the buggy one because it should have
raised an error in the first place.  The most portable way to write a
module #initialize method is to do

def initialize(*a, &b)
  super
  # module init
end

or, if you like to be explicit

def initialize(*a, &b)
  super(*a, &b)
  # module init
end

Because then you can insert it into *any* inheritance hierarchy.  And
your "hacking around" is not necessary.  In cases where the superclass
is Object the real culprit is the child class: that should use
"super()" and not "super" because it knows its super class and knows
the argument list.

I think it is a bad advice to do

def initialize(*a, &b)
  super() # always no args!
  # module init
end

This will _only_ work in cases where the super class or the module
next in inheritance hierarchy does not accept arguments.

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/