Workaround for "conflict" between ARGV and gets?

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”

On Jan 31, 2010, at 20:15 , RichardOnRails wrote:

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

On 1/31/2010 9:15 PM, RichardOnRails wrote:

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

在 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

On Feb 1, 1:34Â am, Jeff P. [email protected] wrote:

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


Jeff P.
Email: [email protected]
Skype: compuperson

ARGV.clear

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

Richard

On Feb 1, 1:24 am, Walton H. [email protected] wrote:

with that directory.
ARGV = [] # Reset ARGV to an empty array
end
puts “EOJ”

puts “EOJ”

Best,
Walton

STDIN.gets.chomp
Very cool: Just tell gets I want the STDIN flavor!!
Many thanks, Walton

Richard

On Feb 1, 1:18 am, Ryan D. [email protected] wrote:

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. 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 :slight_smile:

Best wishes,
Richard

2010/2/1 RichardOnRails [email protected]:

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.

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

irb(main):006:0> method(:gets)
=> #<Method: Object(Kernel)#gets>
irb(main):007:0> method(:gets).owner
=> 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: When will I learn enough Ruby to program
without you guys :slight_smile:

Well, but then you could not have all these nice little chats with us.
:wink:

Cheers

robert

RichardOnRails wrote:

On Feb 1, 1:18�am, Ryan D. [email protected] wrote:

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 :slight_smile:

That depends on you. Good luck! :slight_smile:

Best wishes,
Richard

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs