Hi,
In message "[ruby-talk:20940] calling original method in overrided method?"
on 01/09/06, Henry House <hajhouse / houseag.com> 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.