|
Re: [jruby-user] Iterating over characters in a String ?: msg#00144lang.jruby.user
Ok, I'll take a look at that. So, in the meantime, for iterating over characters, is there a better solution than this ? def each_char(str) 0...str.length do |i| puts str[i] end end This is quite important. I'm writing my app in Brazilian Portuguese, which with all its accents ("Olá, como vai você ?") does not fit in the ASCII subset of UTF-8, and I really need Unicode. []'s Leonel On Nov 16, 2007 2:07 PM, Peter K Chan <peter@xxxxxxxxxx> wrote: > Have you taken a look at Rails' ActiveSupport::Multibyte? It builds some > unicode aware methods on top of the standard string class. It looks to be the > most complete unicode implementation for Ruby now, and there has been talk to > provide a native JRuby implementation of Multibyte to speed up performance in > a JVM. > > http://www.fngtps.com/2006/10/activesupport-multibyte > > Peter > > > -----Original Message----- > From: Leonel Gayard [mailto:leonel.gayard@xxxxxxxxx] > Sent: Friday, November 16, 2007 9:46 AM > To: user@xxxxxxxxxxxxxxxxxx > Subject: [jruby-user] Iterating over characters in a String ? > > I'm looking for some way to iterate over the characters of a String in > Ruby. Ruby's class String does not provide a method each_char, nor > does JRuby's class String. Instead, both provide method each_byte. > > Now, Ruby does not do Unicode. What a shame for a language developed > in Japan ! Instead, it believes that the size of a char is 8 > bits. Thus, Ruby thinks that Matz' name, which is 松本行弘, has 12 > characters: > > matz = '松本行弘' > matz.length -> 12 > > Help. How do I translate the following Java method into JRuby ? > > void m(String s) { > for (int i = 0; i < s.length(); i++) { > System.out.println(s.charAt(i)); > } > } > > The following does *not* work in Ruby, because it outputs each byte, > instead of each letter. > > def each_char(str) > 0...str.length do |i| puts str[i] end > end > > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email > > |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | RE: [jruby-user] Iterating over characters in a String ?: 00144, Peter K Chan |
|---|---|
| Next by Date: | Re: [jruby-user] Iterating over characters in a String ?: 00144, Gregory Murphy |
| Previous by Thread: | RE: [jruby-user] Iterating over characters in a String ?i: 00144, Peter K Chan |
| Next by Thread: | Re: [jruby-user] Iterating over characters in a String ?: 00144, Gregory Murphy |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |