I am running ruby 1.8.6 patchlevel 111 on Windows XP(same problem with
OS X). I am wondering why each_char doesn't work for me? I looked at
the documentation and it shows 'each_char', however when I look at the
results of class.instance_methods.sort, each_char is nowhere to be
found. Any explanations to why I can't use it? Thanks
-Juan
---------------------------------------------------------- Class:
String
A +String+ object holds and manipulates an arbitrary sequence of
bytes, typically representing characters. String objects may be
created using +String::new+ or as literals.
Because of aliasing issues, users of strings should be aware of
the
methods that modify the contents of a +String+ object. Typically,
methods with names ending in ``!'' modify their receiver, while
those without a ``!'' return a new +String+. However, there are
exceptions, such as +String#=+.
Perhaps a better answer to the "why is #each_char defined by jcode?" would be that String (in ruby 1.8) deals with bytes and jcode knows how to treat those bytes as a sequence of characters. This distinction is important as you move to ruby 1.9+ that has better support for treating Strings as characters (with an associated encoding).
Thank you, that explains a lot. I noticed in my 1.8.7 version, I
didn't have any problems. This is what prompted me to find out my
issue. I know I can just write it to get it to work, I was just
wondering why it didn't "work out of the box." Thank you for clearing
this up.
ruby 1.8.7 (2008-06-20 patchlevel 22) [i686-linux]
[user@localhost ~]# irb
irb(main):001:0> st="hithere"
=> "hithere"
irb(main):002:0> st.each_char{|d| puts d}
h
i
t
h
e
r
e
=> "hithere"
irb(main):003:0>