From: "Basile Starynkevitch [news]" Date: 2005-05-19T06:55:14+09:00 Subject: Re: (newbie Q) opposite of inspect for strings On 2005-05-18, Jacob Fugal wrote: > On 5/18/05, Basile Starynkevitch [news] wrote: >> >> I'm desperately seeking a function f such that >> >> b = f(aa) >> >> binds variable b to a 3 character string which is equal to the value >> of a, or I am seeking a method m such that >> >> c = aa.m >> >> binds variable c to a 3 character string (a, tab, b) equal to the >> value of a. >> >> The to_s method is not a valuable substitute for m since aa.to_s is a >> 6 character string (equal to aa) > > What you probably need is eval: A big thanks for this suggestion! > > irb(main):001:0> VERSION > => "1.8.2" > irb(main):002:0> a = "a\tb" > => "a\tb" > irb(main):003:0> aa = a.inspect > => "\"a\\tb\"" > irb(main):004:0> b = eval(aa) > => "a\tb" > irb(main):005:0> a == b > => true > > Of course, you want to be careful when using eval, but it should cut > it for the simple case... Is there some specialized version which does not evaluate its (arbitrary) input, but just unformat a formatted string (and no more, in particular refuse non-string lexemes such as arbitrary [dangerous] ruby expressions)? Regards. -- Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basilestarynkevitchnet aliases: basiletunesorg = bstarynknerimnet 8, rue de la Fa�encerie, 92340 Bourg La Reine, France