On 5/28/10, Nikolai Weibull <now / bitwi.se> wrote:
> What exactly is the convention for writing an #inspect method?  The
> core isn√’ that consistent.  Some definitions return a string in the
> actual read syntax, for example, String.  Others, that lack a read
> syntax, return #<class some-relevant-data> (File::Stat),
> #<class:some-relevant-data> (Dir), or #<class: some-relevant-data>
> (Enumerator).  Finally, some return the same result as #to_s-, for
> example, Time.
>
> Read syntax makes sense.
>
> The #<class convention makes sense, but why is it applied inconsistently?
>
> What is the reasoning behind returning whatever #to_s returns?  Is it
> simply obvious enough what the data the string represents that
> returning something like #<Time: 2007-10-05 16:09:51 UTC> would be
> silly?

inspect is supposed to return a human-readable string representing the
object being inspected. It was never intended (unfortunately) to
return strings which make sense to ruby as well (tho sometimes it
does).

I wrote a library which allows you to turn (almost) any object into an
equivalent inspect-like string. It's much more thorough than inspect;
for instance, it handles user-defined Objects and recursive data
structures correctly.