From: Robert Dober Date: 2009-04-23T20:09:59+09:00 Subject: Re: Parsing String#dump data? On Thu, Apr 23, 2009 at 12:03 PM, Andrew S. Townley wrote: > Hi, > > I was wondering if there was a better/safer way to parse string data > that has been dumped using the String#dump method.  At the moment, I've > been using regular expressions to do it, but that doesn't seem to work > with unicode characters, since they get dumped as follows: > > irb(main):007:0> s = "€" > => "€" > irb(main):008:0> s.dump > => "\"\\342\\202\\254\"" > > On a lark, I figured that Ruby would be able to get them back, so I > tried this: > > irb(main):009:0> x = eval s.dump > => "€" Maybe eval s.dump if %r{\A".*"*\Z} === s.dump && ! %r{#[{@]} === s.dump # not tested is save, but I am not 100% sure Cheers Robert > > Which, of course, works.  However, I'm a bit leery of doing this from a > safety perspective, because I really don't have any control over these > strings, and I'd prefer not to allow the execution of arbitrary Ruby > code every time I'm trying to restore strings (I need them serialized as > appropriately escaped quoted literals). > > Has anyone else ever needed to do this, and, if so, how did you solve > the problem.  I guess I could do another pass on the string looking for > '\\[\n]+' values and try and combine them some way, but I'm not really > sure how to do that either. > > Any ideas? > > Cheers, > > ast > -- > Andrew S. Townley > http://atownley.org > > > -- Si tu veux construire un bateau ... Ne rassemble pas des hommes pour aller chercher du bois, préparer des outils, répartir les tâches, alléger le travail… mais enseigne aux gens la nostalgie de l’infini de la mer. If you want to build a ship, don’t herd people together to collect wood and don’t assign them tasks and work, but rather teach them to long for the endless immensity of the sea. -- Antoine de Saint-Exupéry