Gerald schrieb:
> It seems that the printf conversion to %c tries to call a specific
> method of my class to convert the object to the appropriate format. I am
> a bit confused about the message "can't convert to integer", since the
> to_i method is defined, and sucessfully called that the printf "%d"
> line.
>
> So, two questions arise
>
> - what method am I supposed to provied for printf so it can handle the
> '%s' format string
>
> - How can I figure this out myself. It seems that printf is trying to
> call something, but I have no way of telling exactly what it is
> trying to do. Is there some kind of tracing/debugging that can be
> switched on to see what's printf is trying to do ?
I guessed that it would try to implicitly convert the object to an
Integer. Ruby usually uses to_int() for that. to_i() is used for
explicit conversions.
I verified that guess by doing this:
irb(main):002:0> "%c" % Class.new { def to_int() ?A end }.new
=> "A"
Hope this helps.
--
http://flgr.0x42.net/