It’s not a blog, It’s a feature | Juan A. Vazquez

Jun/08

25

Handling Ruby’s String.each_char Iterator

Ahh the joys of iterators. I can’t say enough about how much they make my life easier. They are just so darn handy. Life was good in my Ruby world until I needed to iterate through the characters in a string. Thinking to myself, “there has got to be a method that does this,” I looked up Ruby’s String class and saw just what the doctor ordered… each_char.

Feeling pretty proud that my favorite language had this baked right in, I was only to happy to inject it into my code and test it out. That is until I recieved the dreaded NoMethodError: undefined method `each_char’ . Eeek, what did I do wrong? Did I mispell the method…..Nope, did I call it correctly…..Yep. Well, what the heck is going on?

After multiple attempts to find the answer on Google, I finally posted my problem to the ruby-talk group. I was told that the Ruby 1.8 String implementation that I was using only understood bytes and that I could use require ‘jcode’ to get the iterator to work the way I wanted. I did some looking around and I am not sure this is a great solution, after all, I could have easily used the each_byte{|f| f.chr} to iterate through and convert accordingly. I don’t understand why something that is documented as part of the class, does not work.

It would really help if there was an easy to search area on the net dedicated to language quirks for people trying to get a better grasp of their programming language of choice. Maybe that will be my next ROR project.

As it turns out, my Ruby 1.8.7 installation on my Fedora core 9 works as advertised.

13 comments

  • Jeremy Logan · July 21, 2008 at 4:03 pm

    Glad to see I’m not the only one having this problem. I agree that it shouldn’t be listed in the Core documentation if it’s, ya know, not in the core.

  • Will · August 15, 2008 at 7:29 am

    Thanks a bunch for the info. Saved me a bunch of searching. each_byte is a good work-around.

  • Gustavo Barrancos · August 15, 2008 at 6:52 pm

    Another guy that bumped this issue! Thanks for the tip!

  • Dawn · December 2, 2008 at 12:31 pm

    Thanks for posting your finding! I was scratching my head on this for awhile.

  • chris · February 6, 2009 at 3:30 pm

    Thanks!

  • ruby_n00b · March 15, 2009 at 2:53 pm

    As 100% of the commenters have commented so far: THANK YOU. I am yet another person who was stuck going cross-eyed over this error. I figure another comment on this post might help google to keep this page “fresh” and high in the google stats. So thanks again!

  • Admin comment by admin · March 15, 2009 at 6:34 pm

    You are all welcome :D

  • brian · March 24, 2009 at 5:12 pm

    Thanks so much for posting about this error in Ruby 1.8.6. I found your posting on the front page of my Google search results [ruby string each_char]. You saved me a lot of time and frustration.

  • EndtheProhibition · May 9, 2009 at 4:42 am

    Thanks man! That’s what I was looking for. :)
    “string”.each_byte { |f| puts f.chr }

  • Marco · May 25, 2009 at 3:51 pm

    Man thank you so much for this, you have no idea how your insight helped me !

    Thanks from Brazil
    Marco

  • Nick · May 31, 2009 at 7:10 pm

    Another “thanks” on the pile!

    Will, unfortunately each_byte can cause problems with multi-byte character encodings.

  • Admin comment by admin · August 15, 2009 at 7:05 am

    @Nick Great comment, I don’t work with a whole lot of multi-byte character encodings. In this case, Jcode would be the way to go for Ruby 1.8. I haven’t played much with Ruby 1.9 but I am excited to play with the better supported encodings that are baked right in.

  • Travis B · September 3, 2009 at 9:14 pm

    If you click on the source in the docs, you get:

    # File lib/jcode.rb, line 209
    def each_char
    if block_given?
    scan(/./m) do |x|
    yield x
    end
    else
    scan(/./m)
    end
    end

    so yeah, just do “string”.scan(/./m)

Leave a Reply

<<

>>

Theme Design by devolux.nh2.me