KEEP IN TOUCH

Join Newsletter

CALL NOW

Share with:


Recent Posts





Ruby 1.8.7 patchlevel inconsistency; “super called outside of method”

I discovered an interesting difference between two patch levels of the same Ruby (1.8.7)

def index
 respond_to do |format|
  format.html {
   super # call the index method of the superclass
  }
 end
end

This does not produce an error in Ruby 1.8.7 patchlevel 72 (the one that happens to ship with Snow Leopard), but in Ruby patchlevel 249 (the most recent one), it tells me I’m not allowed to do this.

super called outside of method

If you think about it from an OOP perspective it actually makes a lot of sense that this would be illegal programming – super is being called from within a block that is passed to respond_to. So, although the intended meaning appears clear from the way the code is written, the Ruby devs probably saw this as something that should be prohibited and fixed Ruby to make it so. Hence, apps written by programmers unaware of this using earlier patch levels may see this after upgrading to the latest Ruby 1.8.7

Post Tags:
5 Comments
  • sax

    Interesting. You could probably assign the parent method to an instance variable and then call that in the block. It makes for more complex code, but it make a lot of sense why you’d not want to call super in a block.

    “a = Proc.new { super }” is kind of fugly.

    February 25, 2010 Reply
  • I’ve see this on a project at work aswell.

    Thanks for pointing it out, good to know someone else has noticed it.

    May 10, 2010 Reply
  • This is not intentional! This is just a bug 😉
    As I understand it was introduced just before p248.
    http://redmine.ruby-lang.org/issues/show/2537
    Should be fixed in the next patchlevel

    May 27, 2010 Reply
  • While I do agree that this isn’t perfect OOP, it is however a very rubyist thing to do, having inherited several legacy apps, this style is everywhere, and is a useful feature that should be kept.

    June 15, 2010 Reply
  • Jason

    Sorry it took me so long to approve your comments! Thanks for the feedback, much appreciated.

    Vitaly & Matt —

    So based on what you’re both say, you’re telling me that the behavior in p249 is wrong and Ruby should allow this syntax? Really my post wasn’t to say which was right or wrong, but just to document it.

    -Jason

    July 20, 2010 Reply

Leave a Comment