Forum: Ruby Un-escaping hexadecimal code

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.
Iñaki Baz C. (Guest)
on 2009-02-20 18:05
(Received via mailing list)
Hi, I receive a String allowing hexadecimal escaping by using %XX
syntax:

  %61lice => alice   ( %61 == a )

I would like to un-escape the string. I've got it using "eval" but I'd
prefer
avoiding using "eval":

  string = "%61lice"
  string = eval %{ "#{ string.gsub(/%/,'\x') }" }
  => "alice"

How could I avoid the usage of "eval" to un-escape the string?

Thanks a lot.
Iñaki Baz C. (Guest)
on 2009-02-20 18:09
(Received via mailing list)
El Viernes, 20 de Febrero de 2009, Iñaki Baz C. escribió:
>
> How could I avoid the usage of "eval" to un-escape the string?

Ops, it is easier than above. CGI lib already does it:

  require 'cgi'
  CGI.unescape("%61lice")
  => "alice"

:)
Jeff S. (Guest)
on 2009-02-20 18:20
(Received via mailing list)
Iñaki Baz C. wrote:

>   string = "%61lice"
>   string = eval %{ "#{ string.gsub(/%/,'\x') }" }
>   => "alice"
>
> How could I avoid the usage of "eval" to un-escape the string?

s.gsub! /%(\d+)/ do |match|
   $1.to_i(16).chr
end
puts s

You might also consider supporting escaped % literals.
Iñaki Baz C. (Guest)
on 2009-02-20 18:38
(Received via mailing list)
El Viernes, 20 de Febrero de 2009, Jeff S. escribió:
> puts s
>
> You might also consider supporting escaped % literals.

It fails since it should be:

  s.gsub! /%(\d\d)/ do |match|
    $1.to_i(16).chr
  end

Without this change it would fail when matching:

  "%61123"


But fixing it then it works well, thanks a lot :)
Iñaki Baz C. (Guest)
on 2009-02-20 18:39
(Received via mailing list)
El Viernes, 20 de Febrero de 2009, Jeff S. escribió:
> puts s
>
> You might also consider supporting escaped % literals.

Also, it should consider not just number but A-F
Jeff S. (Guest)
on 2009-02-20 19:28
(Received via mailing list)
Iñaki Baz C. wrote:
>> puts s
>>
>> You might also consider supporting escaped % literals.
>
> Also, it should consider not just number but A-F

Right you are.
Igor P. (Guest)
on 2009-02-20 20:28
Iñaki Baz C. wrote:
> El Viernes, 20 de Febrero de 2009, Jeff S. escribió:
>> puts s
>>
>> You might also consider supporting escaped % literals.
>
> Also, it should consider not just number but A-F

s = "%4Flice"
s.gsub! /%([\da-fA-F][\da-fA-F])/ { $1.to_i(16).chr }
puts s
Jeff S. (Guest)
on 2009-02-20 21:00
(Received via mailing list)
Igor P. wrote:
> Iñaki Baz C. wrote:
>> El Viernes, 20 de Febrero de 2009, Jeff S. escribió:
>>> puts s
>>>
>>> You might also consider supporting escaped % literals.
>> Also, it should consider not just number but A-F
>
> s = "%4Flice"
> s.gsub! /%([\da-fA-F][\da-fA-F])/ { $1.to_i(16).chr }
> puts s

SyntaxError: compile error
(irb):2: syntax error, unexpected '{', expecting $end
s.gsub! /%([\da-fA-F][\da-fA-F])/ { $1.to_i(16).chr }
                                    ^
Igor P. (Guest)
on 2009-02-20 21:32
Jeff S. wrote:

> SyntaxError: compile error
> (irb):2: syntax error, unexpected '{', expecting $end
> s.gsub! /%([\da-fA-F][\da-fA-F])/ { $1.to_i(16).chr }
>                                     ^

Sorry, in Ruby 1.9 this is no longer a Syntax Error
This topic is locked and can not be replied to.