From: Joe Van Dyk Date: 2005-12-23T09:55:51+09:00 Subject: Re: String > Integer Conversion Problem On 12/22/05, Matthew Feadler wrote: > Jacob Fugal wrote: > > def read_arguments > > arg0, arg1 = ARGV.map{ |n| Integer(n) } > > raise ArgumentError if arg0.nil? or arg0 < 0 > > raise ArgumentError if arg1.nil? or arg1 < 0 > > raise ArgumentError if arg0 > arg1 > > return arg0, arg1 > > rescue ArgumentError > > print <<-USAGE > > Usage > > USAGE > > exit > > end > > > > a, b = read_arguments > > Very nice. I like this construction. However, I believe the above will > throw the exception when I have only one valid arg, which is not what I > want (1 or 2 args is correct; no more, no less). > > So, it should look like this, yes? > > def read_arguments > arg0, arg1 = ARGV.map{ |n| Integer(n) } > raise ArgumentError if (ARGV.length == 0) or (ARGV.length > 2) > raise ArgumentError if (arg0 < 0) or (!arg1.nil? and (arg1 < 0)) > raise ArgumentError if !arg1.nil? and (arg0 > arg1) > return arg0, arg1 > rescue ArgumentError > print <<-USAGE > Usage > USAGE > exit > end > > a, b = read_arguments Write the damn tests, man! :-) If it passes the tests, it should work, unless you have faulty tests, in which case you need more/better tests.