[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

get a portion of a utf8 encoded string

Marco

9/16/2008 7:54:00 AM

hi all.

ruby 1.8.5 (2006-08-25) [i386-linux]

>> puts 'hällo'
hällo
=> nil
>> puts 'hällo'[0, 2]

=> nil
>> puts 'hällo'[0, 3]

=> nil
>> require 'iconv'
=> true
>> puts 'hällo'[0, 3]

=> nil
>> puts 'hällo'[0, 2]

=> nil
>>


The ä character is probably 2 bytes long, so puts 'hällo'[1, 1] returns
only a "pice" of the ä character. Is there a way to make this work?
'hällo'[0, 2] should return -> hä
and
'hällo'[0, 3] should return -> häl

thanks
6 Answers

Heesob Park

9/16/2008 8:24:00 AM

0

SGksCgoyMDA4LzkvMTYgTWFyY28gPG1Aci5jbz46Cj4gaGkgYWxsLgo+Cj4gcnVieSAxLjguNSAo
MjAwNi0wOC0yNSkgW2kzODYtbGludXhdCj4KPj4+IHB1dHMgJ2jDpGxsbycKPiBow6RsbG8KPiA9
PiBuaWwKPj4+IHB1dHMgJ2jDpGxsbydbMCwgMl0KPiBo4paSCj4gPT4gbmlsCj4+PiBwdXRzICdo
w6RsbG8nWzAsIDNdCj4gaMOkCj4gPT4gbmlsCj4+PiByZXF1aXJlICdpY29udicKPiA9PiB0cnVl
Cj4+PiBwdXRzICdow6RsbG8nWzAsIDNdCj4gaMOkCj4gPT4gbmlsCj4+PiBwdXRzICdow6RsbG8n
WzAsIDJdCj4gaOKWkgo+ID0+IG5pbAo+Pj4KPgo+Cj4gVGhlIMOkIGNoYXJhY3RlciBpcyBwcm9i
YWJseSAyIGJ5dGVzIGxvbmcsIHNvIHB1dHMgJ2jDpGxsbydbMSwgMV0gcmV0dXJucyBvbmx5Cj4g
YSAicGljZSIgb2YgdGhlIMOkIGNoYXJhY3Rlci4gSXMgdGhlcmUgYSB3YXkgdG8gbWFrZSB0aGlz
IHdvcms/Cj4gJ2jDpGxsbydbMCwgMl0gc2hvdWxkIHJldHVybiAtPiBow6QKPiBhbmQKPiAnaMOk
bGxvJ1swLCAzXSBzaG91bGQgcmV0dXJuIC0+IGjDpGwKPgo+IHRoYW5rcwo+Cj4KWW91IGNhbiBk
byBpdCB1c2luZyByZWd1bGFyIGV4cHJlc3Npb24gOgoKICdow6RsbG8nLnNwbGl0KC8vdSlbMCwg
Ml0udG9fcyAgLT4gaMOkCgpSZWdhcmRzLAoKUGFyayBIZWVzb2IK

S2

9/16/2008 8:44:00 AM

0

Heesob Park wrote:
> You can do it using regular expression :
>
> 'hällo'.split(//u)[0, 2].to_s -> hä

thanks. even if 'hällo'[0, 2] looks better, this works.

Daniel DeLorme

9/17/2008 4:12:00 AM

0

Heesob Park wrote:
> You can do it using regular expression :
>
> 'hällo'.split(//u)[0, 2].to_s -> hä

Why make it convoluted when ruby makes it so easy to use regexps:

>> puts 'hällo'[/.{2}/um]
hä

Gregory Brown

9/17/2008 11:36:00 PM

0

On Tue, Sep 16, 2008 at 4:50 AM, Pe=F1a, Botp <botp@delmonte-phil.com> wrot=
e:
> From: S2 [mailto:sto.giocando@motor.storm]
> # thanks. even if 'h=E4llo'[0, 2] looks better
>
> upgrade?

It concerns me that people are suggesting using various backwards
incompatible changes in Ruby 1.8.7

Though 1.8.7 may be a good fallback for 1.9 only libraries that may
make things work as expected, I hate the idea of writing code that
works on 1.8.7 but not other versions of Ruby 1.8. At this point, I
feel like 1.8.6 is still the 'real' Ruby 1.8, anyway.

-greg

--=20
Technical Blaag at: http://blog.majesticseacr... | Non-tech
stuff at: http://metametta.bl...

Peña, Botp

9/18/2008 1:36:00 AM

0

From: Gregory Brown [mailto:gregory.t.brown@gmail.com]=20
# On Tue, Sep 16, 2008 at 4:50 AM, Pe=F1a, Botp=20
# <botp@delmonte-phil.com> wrote:
# > From: S2 [mailto:sto.giocando@motor.storm]
# > # thanks. even if 'h=E4llo'[0, 2] looks better
# > upgrade?
# It concerns me that people are suggesting using various backwards
# incompatible changes in Ruby 1.8.7
# Though 1.8.7 may be a good fallback for 1.9 only libraries that may
# make things work as expected, I hate the idea of writing code that
# works on 1.8.7 but not other versions of Ruby 1.8. At this point, I
# feel like 1.8.6 is still the 'real' Ruby 1.8, anyway.

cmon, greg, what could be better than

'h=E4llo'[0, 2]

?

the change is good.

kind regards -botp


Gregory Brown

9/18/2008 3:49:00 PM

0

On Wed, Sep 17, 2008 at 9:36 PM, Pe=F1a, Botp <botp@delmonte-phil.com>

> cmon, greg, what could be better than
>
> 'h=E4llo'[0, 2]
>
> ?
>
> the change is good.

The change is absolutely good! I did a training session at Lone Star
Ruby Conference that sung its praises.

What isn't good is to say "I've not yet updated my code for Ruby 1.9",
so it works on Ruby 1.8.7 only.

My point is that if people really want to use Ruby 1.9 features for
anything but experimentation, they'd do more good by actually using
1.9, not an intermediate release that left most people confused.

If you are writing Ruby 1.8.7 specific code, your code may not run on
Ruby 1.9, for example, the code above will blow up on 1.9 unless the
encoding is properly set.

And your code *definitely* won't work on Ruby 1.8.x aside from 1.8.7

So if you're okay locking to a single point release, that's fine. But
I think it'd help ruby-core a lot more for you to use Ruby 1.9 and
help them iron out issues, and it'd help the Ruby community a lot more
for you to choose whether you are supporting 1.8.x, 1.9.x or both, but
not something that is neither (1.8.7)


-greg



--=20
Technical Blaag at: http://blog.majesticseacr... | Non-tech
stuff at: http://metametta.bl...