Categories
Programming

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

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

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.

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.

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

Leave a Reply

OR LOG IN WITH




Your email address will not be published. Required fields are marked *