Forum: Ruby Euro currency symbol

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9440222a0c3e33f369eef79712e12204?d=identicon&s=25 David Wright (__dw5__)
on 2009-03-31 02:10
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
>> puts "20254".oct
8364


>> puts "20254".oct.chr
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'
£
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-03-31 04:33
David Wright wrote:
> 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
>>> puts "20254".oct
> 8364
>
>
>>> puts "20254".oct.chr
> 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:--
€
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-03-31 04:59
7stud -- wrote:
>
> $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.
9440222a0c3e33f369eef79712e12204?d=identicon&s=25 David Wright (__dw5__)
on 2009-03-31 08:00
7stud -- wrote:
> 7stud -- wrote:
>>
>> $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.


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
------------------------------------------------------------------------
     Returns a string containing the ASCII character represented by the
     receiver's value.
0530f95c50e3eb3177bcba9af4e4de7c?d=identicon&s=25 kungfu-free (Guest)
on 2009-03-31 18:12
(Received via mailing list)
ruby -e 'puts "\244"'


David Wright a écrit :
>>>         ?a.chr    #=> "a"
>> According to the description, you would expect codes above 127 to
> Perl' chr functionality, I should have checked the rdoc for chr,...
> ------------------------------------------------------------------------
>      Returns a string containing the ASCII character represented by the
>      receiver's value.
>
irb(main):019:0> str = "\244"
=> "\244"
irb(main):020:0> puts str
€
=> nil
This topic is locked and can not be replied to.