I know I could use eval
to get the string back like below:
"".inspect == "\"\"" # true
eval("\"\"") == "" # true
But besides eval
, is there an another way to do it?
I know I could use eval
to get the string back like below:
"".inspect == "\"\"" # true
eval("\"\"") == "" # true
But besides eval
, is there an another way to do it?
In your case, ""
is an empty string. "\"\""
is something completely else: A string that, when it is fed to something, yields your ""
empty string. And that something, in this case, is Ruby interpreter. In other words, "\"\""
dump is specifically intended to be eveluated by Ruby interpreter, wheter in eval
, instance_eval
, class_eval
, or ruby
command line, or irb
, or what...
Even if there is another way to do it, that way will only end up emulating Ruby interpreter. So I dare to say, no, it does not make too much sense to do it another way.
But besides eval, is there an another way to do it?
Yes possible,look below using YAML
:
require 'yaml'
YAML.load("\"\"") # => ""
YAML.load("\"\"") == "" # => true