# Argument Error

**URL:** <https://www.ruby-forum.com/t/argument-error/192509>\
**Category:** Ruby\
**Created:** [July 31, 2010, 11:41pm UTC](https://www.ruby-forum.com/t/argument-error/192509 "2010-07-31T23:41:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [July 31, 2010, 11:41pm UTC](https://www.ruby-forum.com/t/argument-error/192509/1 "2010-07-31T23:41:03Z")

</div>

Hi.

I’m learning Ruby and I want to know how to make a script have an  
argument, i.e.

ruby script.rb 3 2

So far I have this:

abort “Correct syntax is ruby [script].rb [number to exponentially  
multiply by] [exponent]” unless ARGV.size == 2  
var = gets.chomp  
var2 = gets.chomp  
puts (var.to\_i \*\* var2)

It’s a simple exponential calculator.

I get this error when trying to run it with 2 arguments (2 and 3).

exponent.rb:2:in `gets': No such file or directory - 2 (Errno::ENOENT) from exponent.rb:2:in`gets’ from exponent.rb:2:in `’

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 1, 2010, 1:14am UTC](https://www.ruby-forum.com/t/argument-error/192509/2 "2010-08-01T01:14:04Z")

</div>

On Sat, Jul 31, 2010 at 11:46 PM, Hd Pwnz0r  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> multiply by] [exponent]" unless ARGV.size == 2  
> var = gets.chomp  
> var2 = gets.chomp

You have the program arguments in the ARGV array, as you probably  
know, since you’ve used it in the previous line:

var = ARGV[0].to\_i  
var2 = ARGV[1].to\_i

(I’ve added the to\_i, since it seems you want to use them as numbers)

> puts (var.to\_i \*\* var2)
> 
> It’s a simple exponential calculator.
> 
> I get this error when trying to run it with 2 arguments (2 and 3).
> 
> exponent.rb:2:in `gets': No such file or directory - 2 (Errno::ENOENT) from exponent.rb:2:in `gets’ from exponent.rb:2:in `’

Jesus.

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 1, 2010, 8:32am UTC](https://www.ruby-forum.com/t/argument-error/192509/3 "2010-08-01T08:32:25Z")

</div>

On Jul 31, 2010, at 3:46 PM 7/31/10, Hd Pwnz0r wrote:

> multiply by] [exponent]" unless ARGV.size == 2

gets reads from an IO stream, not the arguments on the command line:

[http://ruby-doc.org/core/classes/IO.html#M002272](http://ruby-doc.org/core/classes/IO.html#M002272)

The arguments on the command line are stored in the ARGV array. That’s  
why you check ARGV’s size to verify that there are only 2 arguments.

So the code should read the options out of the ARGV array, not trying to  
gets things. There are a couple of ways to do this. My favourite looks  
like:

var = ARGV.shift  
var2 = ARGV.shift

A couple of more complicated ways, but more flexible ways, of getting  
command line arguments are optionparser and getopts. I typically use  
getopts, with no real rationale behind it than that I learned it that  
way first.
