Why does exiting a Ruby thread kill my entire program?

I have this piece of code:

puts "Start" loop do Thread.start do puts "Hello from thread" exit end text = gets puts "#{text}" end puts "Done" 

What I expect is to see “Start”, followed by “Hello from the stream”, and then I can enter the input that will be returned to me back. Instead, I get "Start" and "Hello from the stream," and then the program exits.

From exit documentation:

Terminates execution and plans to start another thread. If this thread is already marked as dead, exit returns Thread. If this is the main thread or the last thread exits the process.

But I thought I created a new topic? Why does this get out of my main process?

+6
source share
1 answer

You are looking at Thread#exit documentation. kill Kernel#exit , which terminates the Ruby script.

 puts "Start" loop do Thread.start do puts "Hello from thread" Thread.exit end text = gets puts "#{text}" end puts "Done" 
+8
source

Source: https://habr.com/ru/post/953115/


All Articles