From: dblack@... Date: 2006-07-30T02:39:18+09:00 Subject: Re: to_i vs. to_int Hi -- On Sun, 30 Jul 2006, Morton Goldberg wrote: > On Jul 29, 2006, at 12:58 PM, dblack@wobblini.net wrote: > >> 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. > > However, > > puts 2 + Integer(S.new) => 4 > > uses your S#to_int True, but it works the same with to_i, as opposed to the string one where to_s and to_str work differently. 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.