Each_char question

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#[]=+.


 User defined methods to be added to String.

Includes:

 Comparable(<, <=, ==, >, >=, between?), Enumerable(all?, any?,
 collect, detect, each_cons, each_slice, each_with_index, entries,
 enum_cons, enum_slice, enum_with_index, find, find_all, grep,
 include?, inject, inject, map, max, member?, min, partition,
 reject, select, sort, sort_by, to_a, to_set, zip)

Constants:

 DeletePatternCache:  {}
 HashCache:           {}
 PATTERN_EUC:         '[\xa1-\xfe][\xa1-\xfe]'
 PATTERN_SJIS:        '[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc]'
 PATTERN_UTF8:        '[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-

\xbf]
[\x80-\xbf]’
RE_EUC: Regexp.new(PATTERN_EUC, 0, ‘n’)
RE_SJIS: Regexp.new(PATTERN_SJIS, 0, ‘n’)
RE_UTF8: Regexp.new(PATTERN_UTF8, 0, ‘n’)
SUCC: {}
SqueezePatternCache: {}
TrPatternCache: {}

Class methods:

 new, yaml_new

Instance methods:

 %, *, +, <<, <=>, ==, =~, [], []=, _expand_ch, _regex_quote,
 block_scanf, capitalize, capitalize!, casecmp, center, chomp,
 chomp!, chop, chop!, concat, count, crypt, delete, delete!,
 downcase, downcase!, dump, each, each_byte, each_char, each_line,
 empty?, end_regexp, eql?, expand_ch_hash, ext, gsub, gsub!, hash,
 hex, include?, index, initialize_copy, insert, inspect, intern,
 is_binary_data?, is_complex_yaml?, iseuc, issjis, isutf8, jcount,
 jlength, jsize, kconv, length, ljust, lstrip, lstrip!, match,
 mbchar?, next, next!, nstrip, oct, original_succ, original_succ!,
 pathmap, pathmap_explode, pathmap_partial, pathmap_replace,

quote,
replace, reverse, reverse!, rindex, rjust, rstrip, rstrip!, scan,
scanf, size, slice, slice!, split, squeeze, squeeze!, strip,
strip!, sub, sub!, succ, succ!, sum, swapcase, swapcase!, to_f,
to_i, to_s, to_str, to_sym, to_yaml, toeuc, tojis, tosjis,
toutf16,
toutf8, tr, tr!, tr_s, tr_s!, unpack, upcase, upcase!, upto

irb
irb(main):001:0> st=“hithere”
=> “hithere”

irb(main):005:0> st.class.each_char{|s| puts s}
NoMethodError: undefined method `each_char’ for String:Class
from (irb):5
from :0
irb(main):011:0> st.class.instance_methods.sort
=> ["%", “*”, “+”, “<”, “<<”, “<=”, “<=>”, “==”, “===”, “=~”, “>”,
“>=”, “[]”, “[]=”, “id”, “send”, “all?”, “any?”, “bet
ween?”, “capitalize”, “capitalize!”, “casecmp”, “center”, “chomp”,
“chomp!”, “chop”, “chop!”, “class”, “clone”, “collect”, “conc
at”, “count”, “crypt”, “delete”, “delete!”, “detect”, “display”,
“downcase”, “downcase!”, “dump”, “dup”, “each”, “each_byte”, “e
ach_line”, “each_with_index”, “empty?”, “entries”, “eql?”, “equal?”,
“extend”, “find”, “find_all”, “freeze”, “frozen?”, “grep”,
“gsub”, “gsub!”, “hash”, “hex”, “id”, “include?”, “index”, “inject”,
“insert”, “inspect”, “instance_eval”, “instance_of?”, “inst
ance_variable_defined?”, “instance_variable_get”,
“instance_variable_set”, “instance_variables”, “intern”, “is_a?”,
“kind_of?”,
“length”, “ljust”, “lstrip”, “lstrip!”, “map”, “match”, “max”,
“member?”, “method”, “methods”, “min”, “next”, “next!”, “nil?”, "
object_id", “oct”, “partition”, “private_methods”,
“protected_methods”, “public_methods”, “reject”, “replace”,
“respond_to?”, “r
everse”, “reverse!”, “rindex”, “rjust”, “rstrip”, “rstrip!”, “scan”,
“select”, “send”, “singleton_methods”, “size”, “slice”, “sl
ice!”, “sort”, “sort_by”, “split”, “squeeze”, “squeeze!”, “strip”,
“strip!”, “sub”, “sub!”, “succ”, “succ!”, “sum”, “swapcase”,
“swapcase!”, “taint”, “tainted?”, “to_a”, “to_f”, “to_i”, “to_s”,
“to_str”, “to_sym”, “tr”, “tr!”, “tr_s”, “tr_s!”, “type”, “unp
ack”, “untaint”, “upcase”, “upcase!”, “upto”, “zip”]
irb(main):012:0>

On Jun 25, 7:52 pm, jvazquez [email protected] wrote:

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

you need to require ‘jcode’

Fred

Cool, it works! what is the reason behind having to use jcode?

On Jun 25, 2:07 pm, Frederick C. [email protected]

On Jun 25, 9:16 pm, jvazquez [email protected] wrote:

Cool, it works! what is the reason behind having to use jcode?

That’s just where those methods are defined.

Fred

where do I look in the future to find where methods are defined?
thanks

On Jun 25, 3:18 pm, Frederick C. [email protected]

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).

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

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>

On Jun 25, 3:39 pm, Rob B. [email protected]