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.
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-02-20 17: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.
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-02-20 17:09
(Received via mailing list)
El Viernes, 20 de Febrero de 2009, Iñaki Baz Castillo 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"

:)
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeff Schwab (Guest)
on 2009-02-20 17:20
(Received via mailing list)
Iñaki Baz Castillo 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.
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-02-20 17:38
(Received via mailing list)
El Viernes, 20 de Febrero de 2009, Jeff Schwab 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 :)
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-02-20 17:39
(Received via mailing list)
El Viernes, 20 de Febrero de 2009, Jeff Schwab escribió:
> puts s
>
> You might also consider supporting escaped % literals.

Also, it should consider not just number but A-F
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeff Schwab (Guest)
on 2009-02-20 18:28
(Received via mailing list)
Iñaki Baz Castillo wrote:
>> puts s
>>
>> You might also consider supporting escaped % literals.
>
> Also, it should consider not just number but A-F

Right you are.
46a8ebdfabc2485407f89a2db5bdafb7?d=identicon&s=25 Igor Pirnovar (rooby)
on 2009-02-20 19:28
Iñaki Baz Castillo wrote:
> El Viernes, 20 de Febrero de 2009, Jeff Schwab 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
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeff Schwab (Guest)
on 2009-02-20 20:00
(Received via mailing list)
Igor Pirnovar wrote:
> Iñaki Baz Castillo wrote:
>> El Viernes, 20 de Febrero de 2009, Jeff Schwab 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 }
                                    ^
46a8ebdfabc2485407f89a2db5bdafb7?d=identicon&s=25 Igor Pirnovar (rooby)
on 2009-02-20 20:32
Jeff Schwab 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.