From: Joe Van Dyk Date: 2005-12-23T07:25:52+09:00 Subject: Re: String > Integer Conversion Problem On 12/22/05, Matthew Feadler wrote: > First, thanks all very much for the lively discussion thus far. It's > proven very useful. > > Timothy Hunter wrote: > > The Integer() method raises ArgumentError if given an empty, > > non-numeric, or otherwise non-well-formed string. > > Hmm...apparently not--at least not w/ 1.8.2 on mswin32. > > This: > > begin > one, two = ARGV.map{ |n| Integer(n) } > rescue ArgumentError > puts "Usage..." > exit > end > > puts one > puts two > > Outputs: > > nil > nil > > When fed no command-line arguments. It does work as intended if any of > the args are non-empty strings, however. > > Also: > > raise "Hey, '#{ arg }' needs to be a number" if !arg.match(/^\d*$/) > > I'm a regexp neophyte, without skill or clue. Any suggestions on > tutorials? Dunno about tutorials, but: the / characters indidicate that whatever is between them is a regular expression. ^ is the beginning of a line (or string) \d is a digit (i.e. 0 1 2 3 4 5 6 7 8 or 9) * means to match 0 or more of the previous thing. $ is the end of the line. So, /^\d*$/ means that the entire string from beginning to end is comprised of zero or more digits. So, a minus sign ('-') or any non-digit somewhere in the string would fail the check.