From: dblack@... Date: 2005-12-22T09:21:39+09:00 Subject: Re: String > Integer Conversion Problem Hi -- On Thu, 22 Dec 2005, Matthew Feadler wrote: > Retro thanks to all who helped me with my last post. I'm certainly more > comfortable with Ruby now than then, but still a newbie as the following > will surely demonstrate. > > Below, you can see that I'm checking command-line arguments to the > program for various conditions. Unfortunately, because the args are > stored as strings, when I convert them via to_i, empty and non-numerical > strings become 0. 0 is an acceptable element in this program, therefore > I can't use it to test for invalid input. I've worked around this (sort > of), by creating two sets of variables for the arguments (one set as > strings, one set as integers). Unfortunately, this complicates the > code, and more importantly, leaves me stumped concerning how to test for > non-numeric values. > > So, the program does what I want, except when the args are non-numeric > strings, and the code seems uglier than it ought to be. In addition to the other suggestions, you might find scanf useful: require 'scanf' arg0, arg1 = ARGV.join.scanf("%d%d") or something like that. David -- David A. Black dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black