String > Integer Conversion Problem

M

Matthew Feadler

Timothy said:
That would be because Integer is never called. If there are no
command-line arguments, ARGV is empty, there's nothing to map.

That makes perfect sense. Thanks.

So, after some experimentation I've got the code listed below. It
successfully checks for:

1.) an empty ARGV (i.e., no command-line args)
2.) more than 2 arguments
3.) non-numeric arguments
4.) negative arguments
5.) first arg greater than second arg

Which is to say, it does everything I want it to. However, I wonder if
it can be optimized, condensed?

Here be code:

$usage = "Usage"

def print_usage val
if val=="exit"
print $usage
exit
else
print $usage
end
end

if ARGV!=[] and !(ARGV.length > 2)
begin
arg0, arg1 = ARGV.map{ |n| Integer(n) }
if (arg0 < 0) or (arg1 < 0)
print_usage("exit")
elsif arg0>arg1
print_usage("exit")
end
rescue ArgumentError
print_usage("exit")
end
else
print_usage("exit")
end
 
M

Matthew Feadler

Matthew said:
Which is to say, it does everything I want it to. However, I wonder if
it can be optimized, condensed?

How 'bout corrected first? Guess my last post is a perfect argument in
favor of my learning how to use test units.

Corrected code be here:

$usage = "Usage:"

def print_usage(val)
if val=="exit"
print $usage
exit
else
print $usage
end
end

if ARGV!=[] and !(ARGV.length > 2)
begin
arg0, arg1 = ARGV.map{ |n| Integer(n) }
if (arg0 < 0) or (!arg1.nil? and (arg1 < 0))
print_usage("exit")
elsif !arg1.nil? and (arg0 > arg1)
print_usage("exit")
end
rescue ArgumentError
print_usage("exit")
end
else
print_usage("exit")
end
 
J

Jacob Fugal

def read_arguments
arg0, arg1 =3D 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 =3D read_arguments

# Jacob Fugal
 
M

Matthew Feadler

Jacob said:
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
 
J

Joe Van Dyk

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 =3D ARGV.map{ |n| Integer(n) }
raise ArgumentError if (ARGV.length =3D=3D 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 =3D 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top