gets give me last arg of script ???

U

Une bévue

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 ???
 
T

ts

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%
 
L

Logan Capaldo

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

here is part of my script :

----------------------------------------------------------------------=20=
--
def get_respons
return (/^[y|Y|O|o].*/ =3D=3D=3D gets.to_s.chomp)
end

dir_path=3DDir.pwd
dir_path=3D$*[0] if $*.length =3D=3D 1
puts "Are you sure you want to convert #{dir_path} ?[y|Y|O|o/n] =20
default
to NO"
if get_respons
puts "reponse YES"
end
----------------------------------------------------------------------=20=
--

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 =20
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].*/ =3D=3D=3D gets.to_s.chomp)"
and 17 the call to "get_respons" : "if get_respons"

notice the $*[0] is allready used in my script ???
--=20
une b=E9vue

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

old gets
 
U

Une bévue

Logan Capaldo said:
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 ;-)
 

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,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top