English 中文(简体)
我如何在不分发我的文字的情况下聆听STDIN的投入?
原标题:How do I listen to STDIN input without pausing my script?
  • 时间:2011-01-13 15:41:17
  •  标签:
  • ruby

I have a while loop consistently listening to incoming connections and outputting them to console. I would like to be able to issue commands via the console without affecting the output. I ve tried:

Thread.new do
    while true
        input   = gets.chomp
        puts "So I herd u sed, "#{input}"."
        #Commands would be in this scope
    end
end

然而,这似乎预留了我的全稿,直到收到投入为止;即便如此,一些读物也随之消失。 我在这样做之前就已经开始了。 I ve Trial look at TCPSocket s select( methods to no effect.

最佳回答

不能确定你想在哪里以你的榜样“继续运作”。 引证这一小篇:

Thread.new do
  loop do
    s = gets.chomp
    puts "You entered #{s}"
    exit if s ==  end 
  end
end

i = 0
loop do
  puts "And the script is still running (#{i})..."
  i += 1
  sleep 1
end

从科技促发所读书是单独阅读的,主稿仍在使用。

问题回答

鲁比使用绿线,因此阻塞系统电话将阻挡所有线索。 想法:

require  io/wait 

while true
  if $stdin.ready?
    line = $stdin.readline.strip
    p "line from stdin: #{line}"
  end
  p "really, I am working here"
  sleep 0.1
end




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签