Complete the code to create a new thread that prints 'Hello from thread'.
thread = Thread.new { puts [1] }The string must be inside quotes to be printed correctly.
Complete the code to start two threads that increment a shared counter.
counter = 0 threads = [] 2.times do threads << Thread.new { counter [1] 1 } end threads.each(&:join) puts counter
The counter should be increased by 1 using the '+=' operator.
Fix the error in the code to safely increment a shared counter using a Mutex.
counter = 0 mutex = Mutex.new threads = [] 3.times do threads << Thread.new do mutex.synchronize { counter [1] 1 } end end threads.each(&:join) puts counter
Inside the mutex block, use '+=' to safely increment the counter.
Fill both blanks to create a hash with word lengths only for words longer than 3 characters.
words = ["cat", "house", "dog", "elephant"] lengths = { word: word.[1] for word in words if word.[2] 3 }
Use 'length' to get word length and '>' to filter words longer than 3.
Fill all three blanks to create a hash with uppercase keys and values greater than 0.
data = { 'a' => 1, 'b' => 0, 'c' => 3 }
result = { [1]: [2] for k, v in data.[3] if v > 0 }Use 'select' to filter, 'k.upcase' for uppercase keys, and 'v' for values.
