Chars method

Any ideas why this code returns :
undefined method `chars’ for “mystring”:String (NoMethodError)

require ‘rubygems’

“mystring”.chars .to_a

Am using Ruby 1.8.6 on ubuntu 8.04

Thank you

GG

George G. 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”]

Thanks Brian

GG

Brian C. wrote:

George G. 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”]

George. If you’re not yet ready to move up to a newer version of Ruby,
you could always add a chars method to String…

class String
def chars
self.split( “” )
end
end

“abcde”.chars
=> [“a”, “b”, “c”, “d”, “e”]

Jim

On Dec 10, 2008, at 7:44 AM, Jim McKerchar wrote:

=> [“a”, “b”, “c”, “d”, “e”]
Note that your method would probably be better named as bytes()
instead of chars():

$ ruby -e ‘p “résumé”.split(“”)’
[“r”, “\303”, “\251”, “s”, “u”, “m”, “\303”, “\251”]

If you wanted to support UTF-8 characters, you could replace split(“”)
with scan(/./mu):

$ ruby -KUe ‘p “résumé”.scan(/./mu)’
[“r”, “é”, “s”, “u”, “m”, “é”]

I’ve written about this on my blog quite a bit, if you are interested:

http://blog.grayproductions.net/articles/understanding_m17n

James Edward G. II

On Wed, Dec 10, 2008 at 9:54 AM, George G.
[email protected] wrote:
Not only 1.9, also 1.8.7
519/20 > ruby -v -e ‘p " ".chars’
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
#Enumerable::Enumerator:0xb7c6ae00
R.


Il computer non è una macchina intelligente che aiuta le persone
stupide, anzi, è una macchina stupida che funziona solo nelle mani
delle persone intelligenti.
Computers are not smart to help stupid people, rather they are stupid
and will work only if taken care of by smart people.

Umberto Eco

Thanks James. I hadn’t thought of that :slight_smile:

Robert D. wrote:

On Wed, Dec 10, 2008 at 9:54 AM, George G.
[email protected] wrote:
Not only 1.9, also 1.8.7
519/20 > ruby -v -e ‘p " ".chars’
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
#Enumerable::Enumerator:0xb7c6ae00
R.

Umberto Eco

Thanks Robert,

From this, I found that my problem can be solved by upgrade my ruby from
version 1.8.6 to ruby 1.8.7 or higher.

Let’s see the ChangeLog of Ruby1.8.7
(http://svn.ruby-lang.org/repos/ruby/tags/v1_8_7_160/ChangeLog)
Mon Apr 14 13:58:32 2008 Akinori MUSHA [email protected]

  • string.c (rb_str_each_char): New methods: String#chars and
    #each_char; backported from 1.9.

And the ChangeLog of Ruby1.8.6
Mon Mar 27 22:19:09 2006 NARUSE, Yui [email protected]

  • ext/nkf/nkf-utf8/{nkf.c, utf8tbl.c, config.h}: imported nkf 2.0.6.
    • Add --ic / --oc option and mapping tables.
    • Add fallback option.
    • Add --no-best-fit-chars option.

Ly