On Jul 26, 2005, at 7:48 AM, Martin Elzen wrote:

>
> Hi all.  Am I the only one that finds it odd that if I define a  
> to_s function for a class, that if I do:
> puts obj
>
> I won't get errors, but if I try something like:
> s = String.new
> s += obj

For that, you need the duck typed version of the method, to_str()...

> class Animal
>
>  def initialize(name)
>    fail "name is nil" if name.nil?
>
>    @name = name
>  end
>
>
>  def to_s
>    "#{@name}"
>  end

alias_method :to_str, :to_s

> end
>
>
>
> c = Animal.new('Snoes')
>
> puts c
>
> puts "substituted in a string literal: #{c}"
>
>
> s = String.new
> s += c

James Edward Gray II