From: Mike Date: 2001-05-29T19:54:04+09:00 Subject: [ruby-talk:15922] Re: is it possible to dynamically coerce objects types in Ruby? On Tue, 29 May 2001 11:18:30 +0900 mirian@cosmic.com (Mirian Crzig Lennox) wrote: > > So this is why "Hello".to_i returns 0, but "Hello".to_int is an error. > The 0 represents how many toes are left after making the shoe fit. :) > So what about a String#to_int method that would raise an exception (or return nil) if the string is not a pure number represented in a string? Something like: class String def to_int raise TypeError, "#{inspect} is not a number" unless self =~ /^[+-]?\d+$/ to_i end end Maybe the same for String#to_float? Mike.