[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

why does . match non-ascii chars?

7stud --

2/23/2009 4:35:00 PM

str = "abcdéf "

result = str.gsub(/./n) do |match|
puts "%%%02X" % match[0]
end
puts


--output:--
%61
%62
%63
%64
%C3
%A9
%66


Doesn't the 'n' option say to match ascii? For what it's worth, I get
the same result without the 'n' option.
--
Posted via http://www.ruby-....

1 Answer

Michael Fellinger

2/24/2009 1:15:00 AM

0

On Tue, Feb 24, 2009 at 1:34 AM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> str =3D "abcd=C3=A9f "
>
> result =3D str.gsub(/./n) do |match|
> puts "%%%02X" % match[0]
> end
> puts
>
>
> --output:--
> %61
> %62
> %63
> %64
> %C3
> %A9
> %66
>
>
> Doesn't the 'n' option say to match ascii? For what it's worth, I get
> the same result without the 'n' option.

The default switch of a regex is actually 'n' already, that only
changes if you set $KCODE before.
It has little influence on what is matched when it comes to '.', but
it influences how the matched bytes will be grouped to resemble
characters.

sigma ~ % ruby -e 'p "abcd=C3=A9f ".scan(/./)'
["a", "b", "c", "d", "\303", "\251", "f", " "]

sigma ~ % ruby -e 'p "abcd=C3=A9f ".scan(/./u)'
["a", "b", "c", "d", "\303\251", "f", " "]

sigma ~ % ruby -Kue 'p "abcd=C3=A9f ".scan(/./u)'
["a", "b", "c", "d", "=C3=A9", "f", " "]

sigma ~ % ruby19 -e 'p "abcd=C3=A9f ".scan(/./)'
["a", "b", "c", "d", "=C3=A9", "f", " "]

Please see some excellent articles about this topic from James Edward Gray =
II:

http://blog.grayproductions.net/articles/bytes_and_characters_...
http://blog.grayproductions.net/categories/character...

^ manveru