George George wrote:
> Any ideas why this code returns :
> undefined method `chars' for "mystring":String (NoMethodError)

Sure - it's because ruby 1.8 has no instance method called "chars" in 
the String class.

Use "ri String" to get a list of methods in String, or look at the API 
reference.

However, there is such a method in ruby 1.9:

$ irb19
irb(main):001:0> "abcde".chars
=> #<Enumerator:0x83b9af4>
irb(main):002:0> "abcde".chars.to_a
=> ["a", "b", "c", "d", "e"]

In ruby 1.8, you could use each_byte to iterate over the bytes, but this 
will give the numeric value for each one.

irb(main):001:0> require 'enumerator'
=> true
irb(main):002:0> "abcde".to_enum(:each_byte).to_a
=> [97, 98, 99, 100, 101]

(Also, note that ruby 1.9 has a different concept of "character" which 
understands multi-byte characters and encodings)

Here's another approach which should work in both 1.8 and 1.9:

irb(main):003:0> "abcde".split(//)
=> ["a", "b", "c", "d", "e"]
-- 
Posted via http://www.ruby-forum.com/.