# What method should a class provide for printf's %c format st

**URL:** <https://www.ruby-forum.com/t/what-method-should-a-class-provide-for-printfs-c-format-st/100486>\
**Category:** Ruby\
**Created:** [July 29, 2006, 10:02am UTC](https://www.ruby-forum.com/t/what-method-should-a-class-provide-for-printfs-c-format-st/100486 "2006-07-29T10:02:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@Gerald](https://www.ruby-forum.com/u/Gerald)\
**Post date:** [July 29, 2006, 10:02am UTC](https://www.ruby-forum.com/t/what-method-should-a-class-provide-for-printfs-c-format-st/100486/1 "2006-07-29T10:02:41Z")

</div>

Consider the following snippet:

* * *

#!/usr/bin/ruby

class Thing

```
    def initialize(name)
            @name = name
    end

    def to_s
            @name
    end

    def to_i
            @name.length
    end

```

end

a = Bla.new(“foo”)

printf “%s\n”, a  
printf “%d\n”, a  
printf “%c\n”, a

* * *

This code breaks at the last line with the message :

./go7:22:in `printf’: can’t convert Thing into Integer (TypeError)

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 ?

Thank you very much,

---

<div class="post-metadata">

**Author:** ![Gerald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@Gerald](https://www.ruby-forum.com/u/Gerald)\
**Post date:** [July 29, 2006, 12:17pm UTC](https://www.ruby-forum.com/t/what-method-should-a-class-provide-for-printfs-c-format-st/100486/2 "2006-07-29T12:17:39Z")

</div>

Florian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > - what method am I supposed to provied for printf so it can handle the
> 
> I verified that guess by doing this:
> 
> irb(main):002:0\> “%c” % Class.new { def to\_int() ?A end }.new  
> =\> “A”
> 
> Hope this helps.

Yes, that was the solution, thank you very much. I must say it is a bit  
confusing that the string and float methods are called to\_s and to\_f,  
that to\_i works for %d, but not for %c, and to\_int works for both %d and  
%c. Can you give me a referer to some official documentation describing  
%these conversion functions ?

Thanks a lot,

---

<div class="post-metadata">

**Author:** ![Gerald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@Gerald](https://www.ruby-forum.com/u/Gerald)\
**Post date:** [July 29, 2006, 11:37am UTC](https://www.ruby-forum.com/t/what-method-should-a-class-provide-for-printfs-c-format-st/100486/3 "2006-07-29T11:37:37Z")

</div>

Gerald schrieb:

> - 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.

---

<div class="post-metadata">

**Author:** ![Gerald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@Gerald](https://www.ruby-forum.com/u/Gerald)\
**Post date:** [August 1, 2006, 3:45am UTC](https://www.ruby-forum.com/t/what-method-should-a-class-provide-for-printfs-c-format-st/100486/4 "2006-08-01T03:45:14Z")

</div>

On Jul 29, 2006, at 6:15 AM, Gerald wrote:

> > > switched on to see what’s printf is trying to do ?  
> > > Hope this helps.  
> > > Thanks a lot,

The rule for things like to\_int, to\_ary and to\_str are basically if  
your object has a canonical representation as an integer it should  
support to to\_int. If it can be represented as an integer in more  
than one way, e.g. (“10” is this a binary 2? a decimal 10? an octal  
8?) you pick a representation and use to\_i. The same rules follow for  
to\_s vs. to\_str and to\_a vs. to\_ary. What this also means is that  
unless your Thing is an _integer_ its likely “bad form” to implement  
to\_int. But like most things in ruby this more a string suggestion  
than anything enforced by the language.
