Justin
Justin

Reputation: 83

Storing threads in a hash

I'm relatively new to Ruby and attempting to store threads so I can refer to them in a hash. Using an array seems to work fine. Here's my code:

require 'thread'

modules = {}
modules[:thread_1] = Thread.new do 
    puts "this is thread_1"
end


modules.each do |thread|
    thread.join # does not work
end

# modules[:thread_1].join # works

I'm assuming there's something simple I'm missing with hashes that I can't seem to find. Thanks!

Upvotes: 1

Views: 54

Answers (1)

spickermann
spickermann

Reputation: 106932

each on an hash yields two elements a key and a value. Try this:

modules.each do |_key, thread|
  thread.join
end

Upvotes: 2

Related Questions