Ruby String.each

While I find Ruby to be a half-assed attempt at an object-oriented Perl, I have been using it quite a bit lately to stay consistent with a lot of existing intellectual property. One of the more maddening things is that somewhere along the way, within the 1.9.x series, the Cardinals of Ruby decided to remove the “each” method from the String object. While logically inconsistent, this method allowed one to create a function that iterated over an array, or if the item passed was a String, iterate over that one item, without extra code to detect if it was “only” a String and handle it differently. Add the below to your rb or include it in a file to get that feature “back”.


class ::String
  def each(&block)
    Array(self).each(&block)
  end
end

*sigh*

This entry was posted in Coding, Linuxy, Rants/Tirades and tagged , . Bookmark the permalink.

1 Response to Ruby String.each

  1. Pingback: Ruby Hash Extensions | Out Of The Clouds

Leave a Reply

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