Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > What method should a class provide for printf's %c format string ?

Reply
Thread Tools

What method should a class provide for printf's %c format string ?

 
 
Gerald
Guest
Posts: n/a
 
      07-29-2006

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,


 
Reply With Quote
 
 
 
 
Florian Gross
Guest
Posts: n/a
 
      07-29-2006
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/


 
Reply With Quote
 
 
 
 
Gerald
Guest
Posts: n/a
 
      07-29-2006
Florian Gross <> wrote:
> 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.


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,


 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      08-01-2006

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

> Florian Gross <> wrote:
>> 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.

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


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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: raw format string in string format method? Chris Angelico Python 3 03-01-2013 12:00 AM
Re: raw format string in string format method? Rick Johnson Python 0 02-28-2013 11:06 PM
Re: raw format string in string format method? Peter Otten Python 0 02-28-2013 03:41 PM
acnt.com provide 2000 new computer hardware products. we provide most powerful computers on the market at reasonable prices. victoria Computer Information 0 10-11-2007 04:25 AM
Simple error : method format(String, Object[]) is not applicable for the arguments (String, String) ankur Java 1 08-27-2007 06:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57