# Gets give me last arg of script?

**URL:** <https://www.ruby-forum.com/t/gets-give-me-last-arg-of-script/64701>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/gets-give-me-last-arg-of-script/64701 "2006-08-15T14:17:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/gets-give-me-last-arg-of-script/64701/1 "2006-08-15T14:17:54Z")

</div>

something i don’t understand : gets give me last arg of script ???

here is part of my script :

* * *

def get\_respons  
return (/^[y|Y|O|o].\*/ === gets.to\_s.chomp)  
end

## dir\_path=Dir.pwd dir\_path=$_[0] if $_.length == 1 puts “Are you sure you want to convert #{dir\_path} ?[y|Y|O|o/n] default to NO” if get\_respons puts “reponse YES” end

and the script (being in my $PATH) is called from terminal :

> set\_date /Users/yvon

and the message error i get is :

> set\_date /Users/yvon  
> Are you sure you want to convert /Users/yvon ?[y|Y|O|o/n] default to NO  
> nil  
> /Users/yvon/bin/set\_date:7:in `gets': Is a directory - /Users/yvon (Errno::EISDIR) from /Users/yvon/bin/set_date:7:in`get\_reponse’  
> from /Users/yvon/bin/set\_date:17

line 7 being “return (/^[y|Y|O|o].\*/ === gets.to\_s.chomp)”  
and 17 the call to “get\_respons” : “if get\_respons”

notice the $\*[0] is allready used in my script ???

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/gets-give-me-last-arg-of-script/64701/2 "2006-08-15T14:17:57Z")

</div>

On Aug 12, 2006, at 6:15 AM, Une bévue wrote:

> ```
> from /Users/yvon/bin/set_date:7:in `get_reponse'
> from /Users/yvon/bin/set_date:17
> 
> ```
> 
> line 7 being “return (/^[y|Y|O|o].\*/ === gets.to\_s.chomp)”  
> and 17 the call to “get\_respons” : “if get\_respons”
> 
> ## notice the $\*[0] is allready used in my script ???
> 
> une bévue

Kernel#gets (what you get when you just use gets) calls ARGF.gets.  
ARGF is a magic IO object that lets your script have a cli api  
similar to apps like ‘cat’. You can check the docs for details, but  
basically what you want to use is $stdin.gets or STDIN.gets not plain  
old gets

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/gets-give-me-last-arg-of-script/64701/3 "2006-08-15T14:17:58Z")

</div>

> > > > > “U” == Une bévue [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

U\> something i don’t understand : gets give me last arg of script ???

It’s normal : it make the right thing.

## moulon% ri Kernel#gets ------------------------------------------------------------ Kernel#gets gets(separator=$/) =\> string or nil

```
 Returns (and assigns to +$_+) the next line from the list of files
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 in +ARGV+ (or +$*+), or from standard input if no files are present
 ^^^^^^^^^^^^^^^^^^
 on the command line. Returns +nil+ at end of file. The optional

```

[…]  
moulon%

U\> notice the $\*[0] is allready used in my script ???

This is bad : $ variables must not be used, it’s best to use ARGV.

ARGV[0] is used but was _not_ removed from ARGV

Try this

moulon% ruby -e ‘p ARGV[0]; p gets’ inexistant  
“inexistant”  
-e:1:in `gets’: No such file or directory - inexistant (Errno::ENOENT)  
from -e:1  
moulon%

moulon% ruby -e ‘p ARGV.shift; p gets’ inexistant  
“inexistant”  
Bonjour  
“Bonjour\n”  
moulon%

–

Guy Decoux

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/gets-give-me-last-arg-of-script/64701/4 "2006-08-15T14:18:01Z")

</div>

Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Kernel#gets (what you get when you just use gets) calls ARGF.gets.  
> ARGF is a magic IO object that lets your script have a cli api  
> similar to apps like ‘cat’. You can check the docs for details, but  
> basically what you want to use is $stdin.gets or STDIN.gets not plain  
> old gets

ok fine thanks 😉

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/gets-give-me-last-arg-of-script/64701/5 "2006-08-15T14:18:00Z")

</div>

Le 12 aoÃ»t 06 Ã 15:50, ts a Ã©crit :

> ARGV[0] is used but was _not_ removed from ARGV
> 
> Try this
> 
> moulon% ruby -e ‘p ARGV[0]; p gets’ inexistant  
> […]
> 
> moulon% ruby -e ‘p ARGV.shift; p gets’ inexistant

ok, fine thanks, that’s clear !
