Euro currency symbol

D

David Wright

Hmm, I seem to be having a bit of a time representing the Euro sign in
Ruby.

ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
(same results on a linux box, 1.8.5 with Ruby)

symbol: €
Decimal: 8364
hex: 20Ac
html number: €
html name: €
description: euro sign


dwright@[1061]:dwright% perl -C2 -le 'print chr(oct("20254"))'
€

dwright@[1062]:dwright%ruby -le 'puts "20254".oct.chr'
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1

dwright@[1063]:dwright%irb
RangeError: 8364 out of char range
from (irb):5:in `chr'
from (irb):5

ruby -e 'puts "20Ac".hex.chr'
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1


ruby -KU -e 'puts "20Ac".hex.chr'
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1

ruby -KU -e '$KCODE="u";puts "20Ac".hex.chr'
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1

ruby -e "puts '20Ac'.hex.chr"
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1

Here's the pound:

ruby -e 'puts "The pound:\243"'
The pound:£

ruby -e 'puts "243".oct.chr'
£
 
7

7stud --

David said:
Hmm, I seem to be having a bit of a time representing the Euro sign in
Ruby.

ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
(same results on a linux box, 1.8.5 with Ruby)

symbol: €
Decimal: 8364
hex: 20Ac
html number: €
html name: €
description: euro sign


dwright@[1061]:dwright% perl -C2 -le 'print chr(oct("20254"))'
€

dwright@[1062]:dwright%ruby -le 'puts "20254".oct.chr'
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1

dwright@[1063]:dwright%irb
RangeError: 8364 out of char range
from (irb):5:in `chr'
from (irb):5

ruby -e 'puts "20Ac".hex.chr'
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1


ruby -KU -e 'puts "20Ac".hex.chr'
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1

ruby -KU -e '$KCODE="u";puts "20Ac".hex.chr'
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1

ruby -e "puts '20Ac'.hex.chr"
-e:1:in `chr': 8364 out of char range (RangeError)
from -e:1

Here's the pound:

ruby -e 'puts "The pound:\243"'
The pound:£

ruby -e 'puts "243".oct.chr'
£


dec_num = "20254".oct
puts dec_num

--output:--
8364


$ri chr
------------------------------------------------------------ Integer#chr
int.chr => string
------------------------------------------------------------------------
Returns a string containing the ASCII character represented by the
receiver's value.

65.chr #=> "A"
?a.chr #=> "a"
230.chr #=> "\346"


There is no ascii character with an ascii code equal to 8364. The pound
has been around a long time, and it made it into extended ascii (or
latin-1, which uses 8 bits). The euro is a recent invention, and it's
numerical code is way out in unicode land.

arr = []
arr << dec_num
str = arr.pack("U") #U=UTF-8 => encode unicode 8364 into a UTF-8
character.
puts str

--output:--
€
 
7

7stud --

7stud said:
$ri chr
------------------------------------------------------------ Integer#chr
int.chr => string
------------------------------------------------------------------------
Returns a string containing the ASCII character represented by the
receiver's value.

65.chr #=> "A"
?a.chr #=> "a"
230.chr #=> "\346"


There is no ascii character with an ascii code equal to 8364. The pound
has been around a long time, and it made it into extended ascii (or
latin-1, which uses 8 bits).

8 bits can be used to store codes between 0-255.

ascii characters are represented by numerical codes between 0-127, so
the text describing the operation of chr in the docs is wrong.
According to the description, you would expect codes above 127 to
produce errors. But codes between 127-255 do not produce errors. The
last example demonstrates that. The docs should read something like:

Returns a string containing the latin-1 (or ISO-8859-1) character
represented by the receiver's value. Valid character codes are 0-255.
 
D

David Wright

7stud said:
8 bits can be used to store codes between 0-255.

ascii characters are represented by numerical codes between 0-127, so
the text describing the operation of chr in the docs is wrong.
According to the description, you would expect codes above 127 to
produce errors. But codes between 127-255 do not produce errors. The
last example demonstrates that. The docs should read something like:

Returns a string containing the latin-1 (or ISO-8859-1) character
represented by the receiver's value. Valid character codes are 0-255.


Thanks, good stuff. I didn't know about Array#pack

Sure, I'm familiar with character sets, I was assuming the 'same as
Perl' chr functionality, I should have checked the rdoc for chr,...

Perl:
chr Returns the character represented by that NUMBER in the
character set. For example, "chr(65)" is "A" in either
ASCII
or Unicode, and chr(0x263a) is a Unicode smiley face.


Ruby:
int.chr => string
 
K

kungfu-free

ruby -e 'puts "\244"'


David Wright a écrit :
Thanks, good stuff. I didn't know about Array#pack

Sure, I'm familiar with character sets, I was assuming the 'same as
Perl' chr functionality, I should have checked the rdoc for chr,...

Perl:
chr Returns the character represented by that NUMBER in the
character set. For example, "chr(65)" is "A" in either
ASCII
or Unicode, and chr(0x263a) is a Unicode smiley face.


Ruby:
int.chr => string
irb(main):019:0> str = "\244"
=> "\244"
irb(main):020:0> puts str

=> nil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,249
Members
46,846
Latest member
BettinaOsw

Latest Threads

Top