Issue #12275 has been updated by tad (Tadashi Saito).


> Still, there is the question of what the encoding of the result of #unescape should be.

Indeed. It is one of few things that I'm still worried about.

For now, `undump` inherits receiver's encoding:

~~~ ruby
"abc".encode('euc-jp').undump.encoding #=> #<Encoding:EUC-JP>
~~~

But it may cause some inconvenient errors like:

~~~ ruby
utf8 = "\xE3\x81\x82".force_encoding('utf-8')
dumped = utf8.dump.encode('ascii') # we can treat dumped string as ASCII
dumped.valid_encoding? #=> always true, of course
dumped.undump #=> RangeError: 12354 out of char range
~~~

`dump`-ed string may contain any codepoints without original encoding information basically,
and this situation reminds me about `Integer#chr(encoding)`.
Then `undump` may needs an argument too, to specify encoding of result string, I think.

(Of course `dumped.force_encoding('utf-8')` before `undump` solves this problem, but I feel it's little redundant.)

Any thoughts about this?

Although this is another topic, I think that the name of this new method is confirmed as 
`#undump` (not `#unescape`) by @matz.  Please see https://bugs.ruby-lang.org/issues/12275#note-6
and below.  (I believe it's a good name because it reminds its spec clearly.)

----------------------------------------
Feature #12275: String unescape
https://bugs.ruby-lang.org/issues/12275#change-67919

* Author: asnow (Andrew Bolshov)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I think it will be usefull to have function that convert input string as it was written in prime qouted string or in double qouted string. It's part of metaprogramming.
Example:

~~~ ruby
class String
  # Create new string like it will be writed in qoutes. Optional argument define type of qouting used: true - prime qoute, false - double qoute. Default is double qoute.
  def unescape prime = false
    eval( prime ? "'#{self}'" : "\"#{self}\"" )
  end
end

"\\\t".unescape # => "\t"
~~~

Other requests:
http://www.rubydoc.info/github/ronin-ruby/ronin-support/String:unescape
http://stackoverflow.com/questions/4265928/how-do-i-unescape-c-style-escape-sequences-from-ruby
http://stackoverflow.com/questions/8639642/best-way-to-escape-and-unescape-strings-in-ruby

Realized
http://www.rubydoc.info/github/ronin-ruby/ronin-support/String:unescape



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>