In article <Pine.LNX.4.30.0112100941570.3130-100000 / candle.superlink.net>,
"David Alan Black" <dblack / candle.superlink.net> wrote:

> I can understand that for Array... but the thing that I found
> un-POLS-like is:
> 
>   class Thing < Array
>     def to_s
>     # ...
>     end
>   end
> 
> and then Thing objects still get puts'd according to the rules of how
> Array is handled.

Unless I've misunderstood, I think the issue is that puts isn't a method of Array (nor of Thing).
Hence, the only way you can override it is at the top level.  Changing
the definition of to_s doesn't change the definition of puts.

I was able to get it to work by defining a specific puts method for Array, which the global
puts calls, like so (I first redefined the current functionality of
puts() and Array to make them work the same way they do now, but allowing
the user to redefine how it works) ...

alias old_puts puts
 
def puts(x)
    if x.type == Array
        x.puts
    else
        old_puts x.to_s
    end
end
 
class Array
    def puts
        self.each { |a| old_puts a }
    end
end
 
a = [1, 2, 3, 4, 5]
 
puts a # => "1\n2\n3\n4\n5" (ie, the normal output)
 
class Array
    def puts
        old_puts self.to_s
    end
 
    def to_s
        "[" + self.join(", ") + "]"
    end
end
 
b = [1, 2, 3, 4, 5]
 
puts b # => "[1, 2, 3, 4, 5]"