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.
George G. (Guest)
on 2008-12-10 08: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
Brian C. (Guest)
on 2008-12-10 10:37
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 G. (Guest)
on 2008-12-10 11:01
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"]
Robert D. (Guest)
on 2008-12-10 11:20
(Received via mailing list)
On Wed, Dec 10, 2008 at 9:54 AM, George G.
<removed_email_address@domain.invalid> 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
Jim McKerchar (Guest)
on 2008-12-10 15: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
James G. (Guest)
on 2008-12-10 16: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 G. II
Jim McKerchar (Guest)
on 2008-12-10 16:52
(Received via mailing list)
Thanks James. I hadn't thought of that :)
Ly V. (Guest)
on 2009-08-06 07:15
Robert D. wrote:
> On Wed, Dec 10, 2008 at 9:54 AM, George G.
> <removed_email_address@domain.invalid> 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  <removed_email_address@domain.invalid>

  * 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  <removed_email_address@domain.invalid>

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