From: Dave Thomas Date: 2001-05-28T02:05:39+09:00 Subject: [ruby-talk:15811] Re: is it possible to dynamically coerce objects types in Ruby? jonas writes: > > Try 'to_int' which is for that particular purpose, not 'to_i' which is > > for explicit conversion to integer. And when you want something to be > > string, try 'to_str'. > > Why is there a difference between to_i and to_int? Is it the principle > of biggest surprise!? :-) The longer forms to to_xxx (to_int, to_str, and to_ary) are used when the argument is _expected_ to be of a particular type. to_i, to_a, and to_s and used to _convert_ something to that type. There's a big difference: to can convert just about anything to a string, but it's not always meaningful to use that value where a string is expected: f = File.new("/dev/null") f.to_s #=>"#" f =~ /wombat/ #=> failed to convert File to String