Forum: Ruby chars method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cd4de09b6a6cdbbf291b5963d6f02efd?d=identicon&s=25 George George (george_g)
on 2008-12-10 07:49
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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-12-10 09:37
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"]
Cd4de09b6a6cdbbf291b5963d6f02efd?d=identicon&s=25 George George (george_g)
on 2008-12-10 10:01
Thanks Brian

GG


Brian Candler wrote:
> 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"]
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-12-10 10:20
(Received via mailing list)
On Wed, Dec 10, 2008 at 9:54 AM, George George
<george.githinji@gmail.com> 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
2ac12620986aa743c74697ddc2854800?d=identicon&s=25 Jim McKerchar (Guest)
on 2008-12-10 14:52
(Received via mailing list)
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2008-12-10 15:05
(Received via mailing list)
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 Gray II
2ac12620986aa743c74697ddc2854800?d=identicon&s=25 Jim McKerchar (Guest)
on 2008-12-10 15:52
(Received via mailing list)
Thanks James. I hadn't thought of that :)
8f642146844c56bd9e8d9b3d39ac94e3?d=identicon&s=25 Ly Vu (ly_vt)
on 2009-08-06 05:15
Robert Dober wrote:
> On Wed, Dec 10, 2008 at 9:54 AM, George George
> <george.githinji@gmail.com> 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_16...)
Mon Apr 14 13:58:32 2008  Akinori MUSHA  <knu@iDaemons.org>

  * 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  <naruse@ruby-lang.org>

  * 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
This topic is locked and can not be replied to.