I want to convert my String object to Enumerable of its 1-sized substrings (not chars), how can I do this efficiently in Ruby?
+1
A:
Maybe I don't understand your question, but there's no difference between a character and a string of 1 character in Ruby.
'hello world'.each_char {|c| puts "substring is #{c}"}
Chuck
2009-02-19 02:40:41
Heh, was about to say the same but my roommate distracted me. =)
wfarr
2009-02-19 02:41:50
Yes there is a difference; "xyzzy"[0] => 120
womble
2009-02-19 02:43:31
In 1.9.1 this will work. In earlier versions, each_char doesn't work and chars are ints.
Dave Ray
2009-02-19 02:46:08
@Dave 1.8.7 has String#each_char too.
womble
2009-02-19 02:48:57
@wombie: 1. That isn't true anymore as of Ruby 1.92. It still doesn't mean a character was different than a one-character string — it's just that String#[] used to return a Fixnum. You have to do "xyzzy"[0].chr to get the appropriate character — which is a one-character string!
Chuck
2009-02-19 02:49:06
As I said, chars were never really ints (just one operator worked that way, and Matz considered it a mistake) and each_char works fine in 1.8.7.
Chuck
2009-02-19 02:51:25
+2
A:
In Ruby 1.9 (and 1.8.7) you can use each_char to reliably iterate over the characters of a string, including proper handling of multi-byte chars and stuff. In earlier releases, each_char does not exist and indexing will return byte codes rather than single char strings. In this case, you can use
"abcdefg".split(//u)
which will split the string in a UTF-8 aware way.
There's some nice discussion here.
Dave Ray
2009-02-19 03:01:36