[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Escape sequences for unicode chars?

Michael Schuerig

6/30/2005 8:15:00 PM


I'm using unicode in a Rails app and there are some cases where I'd like
to use escape sequences for characters instead of the literal
characters.

Case in point is Horizontal Ellipsis, hex 2026. I thought this would be
\x2026, but Ruby only takes the first two characters following \x into
account. Is there some way to make Ruby cooperate?

Michael

--
Michael Schuerig The Fifth Rider of the Apocalypse
mailto:michael@schuerig.de is a programmer.
http://www.schuerig.d...

4 Answers

Austin Ziegler

6/30/2005 8:29:00 PM

0

On 6/30/05, Michael Schuerig <michael@schuerig.de> wrote:
> I'm using unicode in a Rails app and there are some cases where I'd like
> to use escape sequences for characters instead of the literal
> characters.

Ruby does not yet support Unicode literals in any way. You will need
to use the proper UTF-8 encoding bytes as a string. If you can work
with IConv or something, you might be able to use \x20\x26.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Eric Hodel

6/30/2005 8:35:00 PM

0

On 30 Jun 2005, at 13:15, Michael Schuerig wrote:

> I'm using unicode in a Rails app and there are some cases where I'd
> like
> to use escape sequences for characters instead of the literal
> characters.
>
> Case in point is Horizontal Ellipsis, hex 2026. I thought this
> would be
> \x2026, but Ruby only takes the first two characters following \x into
> account. Is there some way to make Ruby cooperate?

You need to encode it into UTF8 or similar:

$ ruby -e 'puts "\342\200\246"'

$

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04



Michael Schuerig

6/30/2005 8:56:00 PM

0

Eric Hodel wrote:

> On 30 Jun 2005, at 13:15, Michael Schuerig wrote:

>> Case in point is Horizontal Ellipsis, hex 2026. I thought this
>> would be
>> \x2026, but Ruby only takes the first two characters following \x
>> into account. Is there some way to make Ruby cooperate?
>
> You need to encode it into UTF8 or similar:
>
> $ ruby -e 'puts "\342\200\246"'
> ?
> $

Thanks! Somehow I didn't expect this to work. Good that it does. I'm
using hex "\xe2\x80\xa6" instead as that's what the nice (unixoid)
unicode command line tool shows, among other things.

Michael

--
Michael Schuerig The Fifth Rider of the Apocalypse
mailto:michael@schuerig.de is a programmer.
http://www.schuerig.d...

why the lucky stiff

6/30/2005 9:42:00 PM

0

Michael Schuerig wrote:

> Thanks! Somehow I didn't expect this to work. Good that it does. I'm
> using hex "\xe2\x80\xa6" instead as that's what the nice (unixoid)
> unicode command line tool shows, among other things.


module Kernel
def u( str )
str.gsub(/U\+([0-9a-fA-F]{4,4})/u){["#$1".hex ].pack('U*')}
end
end

>> u"U+2026"
=> "\342\200\246"
>> "\xe2\x80\xa6"
=> "\342\200\246"

_why