chars method

G

George George

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
 
B

Brian Candler

George said:
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"]
 
G

George George

Thanks Brian

GG


Brian said:
George said:
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"]
 
R

Robert Dober

On Wed, Dec 10, 2008 at 9:54 AM, George George
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.

--=20
Il computer non =E8 una macchina intelligente che aiuta le persone
stupide, anzi, =E8 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
 
J

Jim McKerchar

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
 
J

James Gray

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

class String
def chars
self.split( "" )
end
end

"abcde".chars
=3D> ["a", "b", "c", "d", "e"]

Note that your method would probably be better named as bytes() =20
instead of chars():

$ ruby -e 'p "r=E9sum=E9".split("")'
["r", "\303", "\251", "s", "u", "m", "\303", "\251"]

If you wanted to support UTF-8 characters, you could replace split("") =20=

with scan(/./mu):

$ ruby -KUe 'p "r=E9sum=E9".scan(/./mu)'
["r", "=E9", "s", "u", "m", "=E9"]

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=
 
J

Jim McKerchar

Thanks James. I hadn't thought of that :)

James said:
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"]

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


No virus found in this incoming message.
Checked by AVG - http://www.avg.com
Version: 8.0.176 / Virus Database: 270.9.16/1841 - Release Date: 10/12/2008 09:30
 
L

Ly Vu

Robert said:
On Wed, Dec 10, 2008 at 9:54 AM, George George
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top