I discovered an interesting difference between two patch levels of the same Ruby (1.8.7)
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.
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
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.
I’ve see this on a project at work aswell.
Thanks for pointing it out, good to know someone else has noticed it.
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
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