5

I am new to ruby and trying to work around threads

Let's say I have a method which I want to run every x seconds as follows

def say_hello
    puts 'hello world'
end

I am trying to run it as follows

Thread.new do
    while true do
        say_hello
        sleep(5)
    end
end

But when I run the script, nothing is displayed on the console. What am I missing? Thanks!

4

3 に答える 3

5

Thread オブジェクトを作成していますが、実行が完了するのを待っていません。次を試してください。

Thread.new do
    while true do
        say_hello
        sleep(5)
    end
end.join
于 2013-10-21T15:16:27.463 に答える
0

試す

t1 = Thread.new do
    while true do
        say_hello
        sleep(5)
    end
end

t1.join
于 2013-10-21T15:14:44.560 に答える