coderrr

March 28, 2008

alias_method/module bug in ruby 1.8

Filed under: bug, ruby — Tags: , — coderrr @ 4:53 pm

Yet another bug I’ve found in ruby 1.8

class A
  def a
    :a
  end
end

module M
  def a
    super
  end
  alias_method :b, :a
end

class B < A
  include M
end

p B.new.a  => :a
p B.new.b  # in `b': super: no superclass method `a' (NoMethodError)

If you alias a method defined in a module (aliasing the method inside of the module) then calls to super inside that method will no longer work. The fix for this is to alias the method in the class you’ve included the module in, instead of in the module itself. For the lib I’m writing I have a case where this is not possible so that’s why this bug really sucks for me.

This error only occurs in ruby 1.8, it functions correctly in 1.9.

I’ve added a ticket to rubyforge tracker.

2 Comments »

  1. [...] are a few gotchas Methods that call super which are defined in modules cannot be aliased because of this bug in ruby 1.8. They must be added to an exception list like [...]

    Pingback by Solving the method collision problem of monkey-patching « coderrr — March 31, 2008 @ 2:00 pm

  2. You might want to submit to the tracker on redmine, since that one I think they actually read :)
    -R

    Comment by roger — July 25, 2008 @ 5:19 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.