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
···
--
Posted via http://www.ruby-forum.com/.
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
--
Posted via http://www.ruby-forum.com/.
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/\.
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"]
--
Posted via http://www.ruby-forum.com/\.
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.
On Wed, Dec 10, 2008 at 9:54 AM, George George <george.githinji@gmail.com> wrote:
--
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
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
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.------------------------------------------------------------------------
No virus found in this incoming message.
Checked by AVG - http://www.avg.com Version: 8.0.176 / Virus Database: 270.9.16/1840 - Release Date: 09/12/2008 16:53
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_160/ChangeLog\)
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
--
Posted via http://www.ruby-forum.com/\.
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:
James Edward Gray II
On Dec 10, 2008, at 7:44 AM, Jim McKerchar wrote:
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"]
Thanks James. I hadn't thought of that
James Gray wrote:
On Dec 10, 2008, at 7:44 AM, Jim McKerchar wrote:
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