From: Robert Klemme Date: 2005-11-19T19:22:23+09:00 Subject: Re: Symbol#inspect bug? Eric Mahurin wrote: > On 11/18/05, Eric Hodel wrote: >> >> On Nov 18, 2005, at 4:09 PM, Dominik Bathon wrote: >> >>> On Sat, 19 Nov 2005 00:57:28 +0100, Eric Hodel >>> wrote: >>> >>>> On Nov 18, 2005, at 3:22 PM, Dominik Bathon wrote: >>>> >>>>> Symbol#inspect should return a valid symbol literal, right? >>>> >>>> Not according to the RDoc: >>>> >>>> $ ri Object#inspect >>>> Returns a string containing a human-readable representation >>>> of _obj_. If not overridden, uses the +to_s+ method to >>>> generate >>>> the >>>> string. >>> >>> Yes, but: >>> >>> $ ri Symbol#inspect >>> Returns the representation of sym as a symbol literal. >> >> I consider Object#inspect to set the intent of all the #inspect >> methods, but this may be an oversight. > > For many of the other core classes (and definitely all other immediate > classes), #inspect returns a representation that is eval'able to get > back an equivalent (or the same) object. When possible, I think that > is what #inspect should do. I think it is a bug that Symbol#inspect > almost does it, but not quite (quotes some cases but not others). Although I'd agree that Symbol#inspect can be improved and should be changend (as it's an easy fix) I have a different opinion about the usage of inspect in general: even inspect methods of core classes fail to return something that is proper ruby code. $ ruby -e 'p Object.new' # $ ruby -e 'a=[1];a< I found this problem when doing code generation. For immediate > objects, I embedded the value in the code directly by using #inspect. > I had to put in a hack for Symbol because of the problem discussed. IMHO relying on #inspect for code generation is inappropriate. As the rdoc of Object#inspect indicates it's meant to yield a human readable representation. If you want to generate code you should use another mechanism. For example, you can define a separate method (even if it calls #inspect as default implementation). Or you use YAML / Marshal for complex data structures. Kind regards robert