tags:

views:

149

answers:

3

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
Heh, was about to say the same but my roommate distracted me. =)
wfarr
Yes there is a difference; "xyzzy"[0] => 120
womble
In 1.9.1 this will work. In earlier versions, each_char doesn't work and chars are ints.
Dave Ray
@Dave 1.8.7 has String#each_char too.
womble
@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
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
+5  A: 
"xyzzy".split(//) => ["x", "y", "z", "z", "y"]
womble
+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
To index individual characters in pre-1.9 Ruby, you can either do "hello"[0].chr or "hello"[0,1].
Chuck
As long as they aren't multi-byte :)
Dave Ray