I am preparing a line that will be eval'ed. The string will contain a sentence built from an existing one Array. I have the following:
def stringify(arg)
return "[ '" + arg.join("', '") + "' ]" if arg.class == Array
"'#{arg}'"
end
a = [ 'a', 'b', 'c' ]
eval_str = 'p ' + stringify(a)
eval(eval_str)
which prints a line ["a", "b", "c"].
Is there a more idiomatic way to do this? Array#to_sDon't cut it. Is there a way to assign a result from a method to a pvariable?
Thank!
source
share