From: Renaud HEBERT Date: 2001-09-06T16:04:09+09:00 Subject: [ruby-talk:20952] Re: calling original method in overrided method? Yukihiro Matsumoto wrote: > In message "[ruby-talk:20940] calling original method in overrided method?" > on 01/09/06, Henry House writes: > > |I don't like the default behavior of String#to_i and String#to_f to return > |zero for any string that contains no recognizable numbers. I had thought to > |write overriding methods like so: > | > |Class String > | def to_i > | fail "#{self} is not a valid integer" unless self =~ /^\d*$/ > | # call original to_i > | end > |end > | > |...but I don't how (if it is possible?) to call the original to_i. Am I stuck > |with using a new method name or a custom string class? > > use alias. > > class String > alias original_to_i > def to_i > end > end > > You can use Integer() for strict checked conversion. > > Integer("abc") > # => `Integer': invalid value for Integer: "abc" (ArgumentError) > > matz. I'm curious: why is the default conversion act like that ? I agree with Henry House that a "silent failure" like this is not a good thing.. -- Renaud Hebert phone: (33) 01 30 77 59 92 mailto:renaud.hebert@alcatel.fr