0
0
Rubyprogramming~10 mins

GIL (Global Interpreter Lock) impact 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.new { puts [1] }
Drag options to blanks, or click blank then click option'
A"Hello from thread"
BHello from thread
Cprint 'Hello from thread'
Dputs Hello from thread
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using print instead of puts inside the thread
2fill in blank
medium

Complete the code to start two threads that increment a shared counter.

Ruby
counter = 0
threads = []
2.times do
  threads << Thread.new { counter [1] 1 }
end
threads.each(&:join)
puts counter
Drag options to blanks, or click blank then click option'
A+=
B-=
C*=
D/=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operator like '-=' or '*='
Not incrementing the counter inside the thread
3fill in blank
hard

Fix the error in the code to safely increment a shared counter using a Mutex.

Ruby
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
Drag options to blanks, or click blank then click option'
A/=
B-=
C*=
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operator inside the mutex
Not using mutex to protect the counter
4fill in blank
hard

Fill both blanks to create a hash with word lengths only for words longer than 3 characters.

Ruby
words = ["cat", "house", "dog", "elephant"]
lengths = { word: word.[1] for word in words if word.[2] 3 }
Drag options to blanks, or click blank then click option'
Alength
Bsize
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>'
Using 'size' which is valid but not the expected answer here
5fill in blank
hard

Fill all three blanks to create a hash with uppercase keys and values greater than 0.

Ruby
data = { 'a' => 1, 'b' => 0, 'c' => 3 }
result = { [1]: [2] for k, v in data.[3] if v > 0 }
Drag options to blanks, or click blank then click option'
Ak.upcase
Bv
Cselect
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' instead of 'select' for filtering
Not converting keys to uppercase