# No warning for redefine

**URL:** <https://www.ruby-forum.com/t/no-warning-for-redefine/150082>\
**Category:** Ruby\
**Created:** [October 3, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/no-warning-for-redefine/150082 "2008-10-03T13:48:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hanholl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a87d85/32.png) [@hanholl](https://www.ruby-forum.com/u/hanholl)\
**Post date:** [October 3, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/no-warning-for-redefine/150082/1 "2008-10-03T13:48:47Z")

</div>

Hi,

Shouldn’t the following:

#!/usr/bin/ruby -w

module Mixin  
def function  
puts ‘Mixin’  
end  
end  
include Mixin  
def function  
puts ‘Main’  
end

function

# end of demo.rb

produce something like:

demo.rb:9: warning: method redefined; discarding old function

Behaviour now is to discard the mixed-in method silently.

Cheers,

Han H.

---

<div class="post-metadata">

**Author:** ![hanholl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a87d85/32.png) [@hanholl](https://www.ruby-forum.com/u/hanholl)\
**Post date:** [October 3, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/no-warning-for-redefine/150082/2 "2008-10-03T14:00:31Z")

</div>

Hi –

On Fri, 3 Oct 2008, Han H. wrote:

> end  
> demo.rb:9: warning: method redefined; discarding old function
> 
> Behaviour now is to discard the mixed-in method silently.

It’s not discarded; it’s still there (in the module) and you can use  
it with other classes, or reach it through ‘super’ in the override.  
Discarding means you can never get to a method again:

class C  
def m  
end  
def m # Previous m is now completely uncallable  
end  
end

David

---

<div class="post-metadata">

**Author:** ![hanholl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a87d85/32.png) [@hanholl](https://www.ruby-forum.com/u/hanholl)\
**Post date:** [October 3, 2008, 4:06pm UTC](https://www.ruby-forum.com/t/no-warning-for-redefine/150082/3 "2008-10-03T16:06:19Z")

</div>

> It’s not discarded; it’s still there (in the module) and you can use  
> it with other classes, or reach it through ‘super’ in the override.  
> Discarding means you can never get to a method again:

Of course! How stupid of me.

Thanks,

Han H.

---

<div class="post-metadata">

**Author:** ![hanholl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a87d85/32.png) [@hanholl](https://www.ruby-forum.com/u/hanholl)\
**Post date:** [October 4, 2008, 5:33am UTC](https://www.ruby-forum.com/t/no-warning-for-redefine/150082/4 "2008-10-04T05:33:16Z")

</div>

David A. Black wrote:

> On Fri, 3 Oct 2008, Han H. wrote:
> 
> > Shouldn’t the following:  
> > [snip]
> 
> > produce something like:
> > 
> > demo.rb:9: warning: method redefined; discarding old function
> > 
> > Behaviour now is to discard the mixed-in method silently.
> 
> It’s not discarded; it’s still there (in the module) and you can use  
> it with other classes, or reach it through ‘super’ in the override.

But since the override doesn’t use super, in effect it has the same  
effect as discarding the included ‘function’, at least as far as the  
main object is concerned. I wish ruby would output a warning for cases  
like this, i.e. “warning: method `foo’ ignores method defined in  
/path/to/file.rb:123”

---

<div class="post-metadata">

**Author:** ![hanholl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a87d85/32.png) [@hanholl](https://www.ruby-forum.com/u/hanholl)\
**Post date:** [October 4, 2008, 5:56am UTC](https://www.ruby-forum.com/t/no-warning-for-redefine/150082/5 "2008-10-04T05:56:18Z")

</div>

Daniel DeLorme wrote:

> David A. Black wrote:
> 
> > On Fri, 3 Oct 2008, Han H. wrote:
> > 
> > > Shouldn’t the following:  
> > > [snip]
> > 
> > > produce something like:
> > > 
> > > demo.rb:9: warning: method redefined; discarding old function
> > > 
> > > Behaviour now is to discard the mixed-in method silently.
> > 
> > It’s not discarded; it’s still there (in the module) and you can use  
> > it with other classes, or reach it through ‘super’ in the override.
> 
> But since the override doesn’t use super, in effect it has the same  
> effect as discarding the included ‘function’, at least as far as the  
> main object is concerned. I wish ruby would output a warning for cases  
> like this, i.e. “warning: method `foo’ ignores method defined in  
> /path/to/file.rb:123”

The behavior is unsurprising if you think of inheritance when you see  
Module inclusion. If on the other hand if your mental model is “copying  
methods” from the Module into the target then I can see the confusion.

You don’t expect warnings when a derived class overrides a base class  
method now do you?

–Ragav

---

<div class="post-metadata">

**Author:** ![hanholl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a87d85/32.png) [@hanholl](https://www.ruby-forum.com/u/hanholl)\
**Post date:** [October 4, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/no-warning-for-redefine/150082/6 "2008-10-04T16:18:55Z")

</div>

Hi –

On Sat, 4 Oct 2008, Daniel DeLorme wrote:

> > > Behaviour now is to discard the mixed-in method silently.
> > 
> > It’s not discarded; it’s still there (in the module) and you can use  
> > it with other classes, or reach it through ‘super’ in the override.
> 
> But since the override doesn’t use super, in effect it has the same effect as  
> discarding the included ‘function’, at least as far as the main object is  
> concerned. I wish ruby would output a warning for cases like this, i.e.  
> “warning: method `foo’ ignores method defined in /path/to/file.rb:123”

Yikes. Be careful what you wish for 🙂 That would put a damper on a  
lot of code, as well as slowing things down (the ‘super’ determination  
could only be made at runtime) and spewing endless errors. For  
example:

{}.select …

would give you a warning, because the Hash override of  
Enumerable#select does not call super.

There’s absolutely nothing fishy or wrong with not calling super, so  
there’s nothing to warn about. The whole object/class model of Ruby is  
based on the idea of a method search-path, to which you can prepend  
classes and modules precisely to prevent the execution of earlier  
definitions of methods. Any issues with name clashing and such (of  
which there shouldn’t be many, if any, since you’d presumably be  
familiar with the class you’re inheriting from) would be exposed by  
tests.

David

---

<div class="post-metadata">

**Author:** ![hanholl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a87d85/32.png) [@hanholl](https://www.ruby-forum.com/u/hanholl)\
**Post date:** [October 4, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/no-warning-for-redefine/150082/7 "2008-10-04T16:45:51Z")

</div>

Ragav S. wrote:

> > > It’s not discarded; it’s still there (in the module) and you can use
> 
> You don’t expect warnings when a derived class overrides a base class  
> method now do you?

Not if the derived class invokes ‘super’. But if it doesn’t, it can  
indicate a problem. For example, maybe you coincidentally chose a method  
name that clashes with the superclass. Or maybe someone later adds a  
method with the same name to the superclass (e.g. initialize).

For 1.9, a different method lookup scheme for private methods was  
proposed to address that problem. Ultimately it was too big of a change  
and didn’t make it into 1.9, but I think it’s a recognized problem,  
especially when mixing libraries that extend the core classes.
