So I had the idea to reuse something I already saw once. I write a new file named compat18.rb and include it within the sources that need it. Ruby makes it very easy to add new methods to existing classes/modules anyway, even if they exist already, so I just did it and it works like a charm.
Here is a small snippet:
class Array def find_index(idx) index(idx) end end class Dir def exists?(path) File.directory?(path) end end
Update: It can happen that a fallback method from Ruby 1.8 has been totally dropped and replaced against a new method in 1.9, and in this case the older method has to be checked if it exists, and otherwise make a call to the parent.
class Array def count if defined? nitems return nitems else return super end end end
No comments:
Post a Comment