From: Xiangrong Fang Date: 2003-07-16T01:53:12+09:00 Subject: Re: How to reduce Ruby runtime error? Hi Hal, Thank you for the suggestions. The last thing I want to do is to raise exceptions! If I raised exception, the program is broken. I will have to deal with that manually. Let me give you an example. I have a input line: line = "a b" I used a=line.split(" "), it becomes a=["a", "b"], that's what I want. However, in real environment, some time the input is: line = "c" then, a=["c"]. In this case, if my program write somethin like if a[1].upcase == "GOOD" then... a[1] is nil, and you can't do "upcase" with it! It is too tiring to find out all such possible errors, and raise exceptions. Actually, your "Not Recommended" ideas is better for me: > Not recommended: You could convert a nil value on entry > into the method: > > x = [] if x.nil? > > Not recommended: You could modify the NilClass so that a > nil value acted the way you want: > > class NilClass > def +(value) > value # nil + x always equals x > end > end I want my program to be robust. So the above recommendation is a good idea. I don't know why it is "not recommended"? Is it possible to "overload" the Nil class so that it is polymophic? for example, if the method upcase is called, nil act like string, if the expression a = 2 + nil is executed, nil act like zero? That will be fantastic. Thanks for your help. Shannon On Wed, 16 Jul 2003 01:20:49 +0900 "Hal E. Fulton" wrote: > ----- Original Message ----- > From: "Xiangrong Fang" > To: "ruby-talk ML" > Sent: Tuesday, July 15, 2003 11:05 AM > Subject: How to reduce Ruby runtime error? > > > > Hi my friends, > > > > I am thinking of how can I reduce Ruby runtime error. Not like > > languages such as C or Pascal, Ruby is typeless. This is a good thing, > > especially in design time. However, I found that my application is not > > stable due to this feature. > > > > For example, a routine is expecting a string, and this string turned out > > to be nil at runtime, an error occurred. In Delphi, for example, it is > > impossible that a string becomes a nil, it will be an empty string. > > > > How can I better utilize Ruby's typeless feature yet avoid such anonying > > runtime problem? > > You can always check types explicitly if you > really want to: > > raise "Expecting an array" if not x.is_a? Array > > Or you could test for nil: > > raise "Argument was nil" if x.nil? > > You can test for methods available ("duck typing") > rather than actual class: > > raise "Object has no 'push' method" if not x.respond_to? :push > > You can check for the "magic" conversion methods such as > to_str and to_ary: > > if not x.is_a? String > x = x.to_str if x.respond_to? :to_str > else > raise "Wanted a string" > end > > Not recommended: You could convert a nil value on entry > into the method: > > x = [] if x.nil? > > Not recommended: You could modify the NilClass so that a > nil value acted the way you want: > > class NilClass > def +(value) > value # nil + x always equals x > end > end > > I'm sure there are other ways. > > Hal > > -- > Hal Fulton > hal9000@hypermetrics.com > > -- Xiangrong Fang