4 Replies - 3933 Views - Last Post: 21 June 2012 - 03:49 PM Rate Topic: -----

#1 marscom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-March 12

Breaking loop upon user entering "stop"

Posted 25 March 2012 - 12:58 AM

Hey all, just a simple question.

I have a loop that I am using in ruby and I would like to be able to break from this loop whenever I want to. I would like to be able to enter "stop" into the console and hit return and have the loop break.

The loop needs to loop as normal until this happens and should only stop when I type the command.

Also, I have had a few solutions that cause the loop to go as such

loop
input
loop
input
etc...

But I need

loop
loop
loop
ect...
input
stop.

Cheers in advance!
Martin

Is This A Good Question/Topic? 0
  • +

Replies To: Breaking loop upon user entering "stop"

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2088
  • View blog
  • Posts: 3,176
  • Joined: 21-June 11

Re: Breaking loop upon user entering "stop"

Posted 25 March 2012 - 03:27 AM

loop do
  input = gets.chomp
  if input == "stop"
    break
  else
    do_something_with(input)
  end  
end


Was This Post Helpful? 0
  • +
  • -

#3 marscom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-March 12

Re: Breaking loop upon user entering "stop"

Posted 25 March 2012 - 04:38 AM

Hey sepp, thanks for the assistance, unfortunatly this does not resolve my issue. If you check the posting, this does what the 1st example does, looping, while stopping for input, then continues looping - I need it to loop repeatedly and stop looping once the command has been entered. Any more help would be awesome!

Cheers
Martin
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2088
  • View blog
  • Posts: 3,176
  • Joined: 21-June 11

Re: Breaking loop upon user entering "stop"

Posted 25 March 2012 - 05:09 AM

Ah, I see what you mean now. You can do that using threads:

input = nil
worker_thread = Thread.new do
  while input != "stop"
    puts "Looping"
  end
end

input = gets.chomp
worker_thread.join



This only asks for input once and if the user enters something other than "stop", it will loop forever. If you don't want that, you can put the input = gets.chomp inside a loop as well.
Was This Post Helpful? 1
  • +
  • -

#5 Gazuu  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 18
  • Joined: 20-June 12

Re: Breaking loop upon user entering "stop"

Posted 21 June 2012 - 03:49 PM

My approach would have looked something like this:

running = true         # main loop running?

t = Thread.new do
  input = ""
  while input != "stop"
    puts "Enter command:"
    input = gets.chomp
    running == false if input == "stop"
  end
end

# main loop
while running
  # whatever you want to do in your loop
end



I'm curious and just want to know myself if this would be a good/bad approach and why, I'm not yet THAT familiar with ruby threads and they're actually giving me a hard time in my own code now. (that's because it got to do with networking, getting kinda complex)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1