From: dblack@... Date: 2006-07-30T01:58:29+09:00 Subject: Re: to_i vs. to_int Hi -- On Sun, 30 Jul 2006, gabriele renzi wrote: > Ico ha scritto: >> Hello, >> >> I have a class which instances can be represented as integers. What is >> the difference between the to_i and to_int methods, and when is which >> form needed ? > > short names (to_x) are for common programmer use. > Long names (to_xyz) are there so that ruby can do some implicit conversions > between things that can basically be threated as xyz. > > So in your case it could be meaningful to define one and alias the other to > it. What's a use case for to_int? I was thinking of this classic to_str example: class S def to_str "some text" end end puts "Here is " + S.new # Here is some text But the to_int equivalent doesn't work: class S def to_int 2 end end puts 2 + S.new # TypeError: S can't be coerced into Fixnum Same thing with: puts Integer(2) + S.new I'm stuck trying to find a case where to_int does what to_str does -- but maybe it doesn't. David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.