0
0
Rubyprogramming~10 mins

Thread creation and execution in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new thread that prints 'Hello from thread!'.

Ruby
thread = Thread.[1] do
  puts 'Hello from thread!'
end
thread.join
Drag options to blanks, or click blank then click option'
Acreate
Bnew
Cstart
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread.start which does not exist
Using Thread.run which runs the current thread
Using Thread.create which is not a Ruby method
2fill in blank
medium

Complete the code to wait for the thread to finish before continuing.

Ruby
thread = Thread.new do
  puts 'Working in thread'
end
[1]
Drag options to blanks, or click blank then click option'
Athread.join
Bthread.wait
Cthread.sleep
Dthread.stop
Attempts:
3 left
💡 Hint
Common Mistakes
Using thread.stop which pauses the thread
Using thread.wait which does not exist
Using thread.sleep which pauses the current thread
3fill in blank
hard

Fix the error in the code to properly create and start a thread that prints numbers.

Ruby
thread = Thread.[1] do
  3.times { |i| puts i }
end
thread.join
Drag options to blanks, or click blank then click option'
Arun
Bstart
Cnew
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread.start which is not a Ruby method
Using Thread.run which runs the current thread
Using Thread.create which does not exist
4fill in blank
hard

Fill both blanks to create a thread that prints squares of numbers from 1 to 3 and waits for it to finish.

Ruby
thread = Thread.[1] do
  (1..3).each { |n| puts n[2]2 }
end
thread.join
Drag options to blanks, or click blank then click option'
Anew
B**
C*
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of ** for exponentiation
Using Thread.start which does not exist
5fill in blank
hard

Fill both blanks to create a thread that prints uppercase letters from an array and waits for it to finish.

Ruby
letters = ['a', 'b', 'c']
thread = Thread.[1] do
  letters.each { |letter| puts letter.[2] }
end
thread.join
Drag options to blanks, or click blank then click option'
Anew
Bupcase
Ccapitalize
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using capitalize which only capitalizes the first letter
Using start which is not a Ruby method for threads