Workaround for "conflict" between ARGV and gets?

R

RichardOnRails

gets looks to ARGV, if populated, for its source of data. That is its
documented behavior.

I want to:
1) use ARGV to point to a directory I want to process and
2) use gets to inquire whether the user wants the program to proceed
with that directory.

I thought I might be able to reset ARGV to an empty array after I
saved its content, but that doesn't work. Is there a way around this.

The following code never pauses to allow the user to enter yes/no, as
I intended it to. Is there a workaround?

=== Code ==
puts "ARGV = %s" % ARGV.join(", ")
argv = ARGV[0] # Save ARGV's content
ARGV = [] # Reset ARGV to an empty array
puts "Delete indicated item [yes, no]"
STDOUT.flush
response = gets.chomp # doesn't go to STDIN !!!
puts case response
when /^yes$/i; "Deleting"
when /^no$/i; "Quiting"
else; "Huh?"
end
puts "EOJ"

puts "EOJ"
 
R

Ryan Davis

gets looks to ARGV, if populated, for its source of data. That is its
documented behavior.

I want to:
1) use ARGV to point to a directory I want to process and
2) use gets to inquire whether the user wants the program to proceed
with that directory.

I thought I might be able to reset ARGV to an empty array after I
saved its content, but that doesn't work. Is there a way around this.

The following code never pauses to allow the user to enter yes/no, as
I intended it to. Is there a workaround?

=== Code ==
puts "ARGV = %s" % ARGV.join(", ")
argv = ARGV[0] # Save ARGV's content
ARGV = [] # Reset ARGV to an empty array
puts "Delete indicated item [yes, no]"
STDOUT.flush
response = gets.chomp # doesn't go to STDIN !!!

ri IO.gets
ri Kernel.gets
 
W

Walton Hoops

gets looks to ARGV, if populated, for its source of data. That is its
documented behavior.

I want to:
1) use ARGV to point to a directory I want to process and
2) use gets to inquire whether the user wants the program to proceed
with that directory.

I thought I might be able to reset ARGV to an empty array after I
saved its content, but that doesn't work. Is there a way around this.

The following code never pauses to allow the user to enter yes/no, as
I intended it to. Is there a workaround?

=== Code ==
puts "ARGV = %s" % ARGV.join(", ")
argv = ARGV[0] # Save ARGV's content
ARGV = [] # Reset ARGV to an empty array
puts "Delete indicated item [yes, no]"
STDOUT.flush
response = gets.chomp # doesn't go to STDIN !!!

Change this line to 'STDIN.gets.chomp'. Finit!
puts case response
when /^yes$/i; "Deleting"
when /^no$/i; "Quiting"
else; "Huh?"
end
puts "EOJ"

puts "EOJ"

Best,
Walton
 
J

Jeff Peng

在 2010-02-01一的 13:15 +0900,RichardOnRails写é“:
gets looks to ARGV, if populated, for its source of data. That is its
documented behavior.

I changed the script to:

dir=ARGV[0]
ARGV.clear
puts "Delete indicated item [yes, no]"
STDOUT.flush
response = gets.chomp # doesn't go to STDIN !!!
puts case response
when /^yes$/i; "Deleting"
when /^no$/i; "Quiting"
else; "Huh?"
end
puts "EOJ"


It works fine for me.

$ ruby pause.rb 323
Delete indicated item [yes, no]
yes
Deleting
EOJ
 
R

RichardOnRails

在 2010-02-01一的 13:15 +0900,RichardOnRails写é“:
gets looks to ARGV, if populated, for its source of data.  That isits
documented behavior.

I changed the script to:

dir=ARGV[0]
ARGV.clear
puts "Delete indicated item [yes, no]"
STDOUT.flush
response = gets.chomp   # doesn't go to STDIN !!!
puts case response
  when /^yes$/i; "Deleting"
  when /^no$/i; "Quiting"
  else; "Huh?"
  end
puts "EOJ"

It works fine for me.

$ ruby pause.rb 323
Delete indicated item [yes, no]
yes
Deleting
EOJ

--
Jeff Peng
Email: (e-mail address removed)
Skype: compuperson
ARGV.clear

Excellent. My heavy-handed method pales in light of your solution.
Many thanks, Jeff
-
Richard
 
R

RichardOnRails

gets looks to ARGV, if populated, for its source of data.  That is its
documented behavior.
I want to:
1)  use ARGV to point to a directory I want to process and
2)  use gets to inquire whether the user wants the program to proceed
with that directory.
I thought I might be able to reset ARGV to an empty array after I
saved its content, but that doesn't work.  Is there a way around this..
The following code never pauses to allow the user to enter yes/no, as
I intended it to.  Is there a workaround?
=== Code ==
puts "ARGV = %s" % ARGV.join(", ")
argv = ARGV[0]                     # Save ARGV's content
ARGV = []                          # Reset ARGV to an empty array
puts "Delete indicated item [yes, no]"
STDOUT.flush
response = gets.chomp      # doesn't go to STDIN !!!

Change this line to 'STDIN.gets.chomp'.  Finit!
puts case response
   when /^yes$/i; "Deleting"
   when /^no$/i; "Quiting"
   else; "Huh?"
   end
puts "EOJ"
puts "EOJ"
Best,
Walton
STDIN.gets.chomp
Very cool: Just tell gets I want the STDIN flavor!!
Many thanks, Walton
-
Richard
 
R

RichardOnRails

gets looks to ARGV, if populated, for its source of data.  That is its
documented behavior.
I want to:
1)  use ARGV to point to a directory I want to process and
2)  use gets to inquire whether the user wants the program to proceed
with that directory.
I thought I might be able to reset ARGV to an empty array after I
saved its content, but that doesn't work.  Is there a way around this..
The following code never pauses to allow the user to enter yes/no, as
I intended it to.  Is there a workaround?
=== Code ==
puts "ARGV = %s" % ARGV.join(", ")
argv = ARGV[0]                     # Save ARGV's content
ARGV = []                          # Reset ARGV to an empty array
puts "Delete indicated item [yes, no]"
STDOUT.flush
response = gets.chomp      # doesn't go to STDIN !!!

ri IO.gets
ri Kernel.gets

I see, Ryan. What I should have done to understand the problem fully
was:

puts gets.class # => String

which makes it clear why I didn't get the 'native' versions you
pointed out. I had checked in http://www.ruby-doc.org/core/ but made
two stupid mistakes: I failed to notice Kernel in the middle column
and gets (Kernel) in the right-hand column. And I never thought of
IO, nor noted gets (IO) in the right-hand column.

It all begs the question: When will I learn enough Ruby to program
without you guys :)

Best wishes,
Richard
 
M

Marnen Laibow-Koser

RichardOnRails said:
with that directory.
ARGV = [] � � � � � � � � � � � � �# Reset ARGV to an empty array
puts "Delete indicated item [yes, no]"
STDOUT.flush
response = gets.chomp � � �# doesn't go to STDIN !!!

ri IO.gets
ri Kernel.gets

I see, Ryan. What I should have done to understand the problem fully
was:

puts gets.class # => String

which makes it clear why I didn't get the 'native' versions you
pointed out.

No it doesn't. All it tells you is that gets returns a String.

[...]
It all begs the question: When will I learn enough Ruby to program
without you guys :)

That depends on you. Good luck! :)
Best wishes,
Richard

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 
R

Robert Klemme

2010/2/1 RichardOnRails said:
I see, Ryan. =A0What I should have done to understand the problem fully
was:

puts gets.class # =3D> String

which makes it clear why I didn't get the 'native' versions you
pointed out. =A0I had checked in http://www.ruby-doc.org/core/ but made
two stupid mistakes: I failed to notice Kernel in the middle column
and gets (Kernel) in the right-hand column. =A0And I never thought of
IO, nor noted gets (IO) in the right-hand column.

Here's something you can do to find out more about a method:

irb(main):006:0> method:)gets)
=3D> #<Method: Object(Kernel)#gets>
irb(main):007:0> method:)gets).owner
=3D> Kernel

Then you can do "ri Kernel#gets" and get the docs (or look online at
http://www.ruby-doc.org/ of course).
It all begs the question: =A0When will I learn enough Ruby to program
without you guys :)

Well, but then you could not have all these nice little chats with us. ;-)

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top