Bird
Raised Fist0
Rubyprogramming~10 mins

GIL (Global Interpreter Lock) impact in Ruby - Interactive Code Practice

Choose your learning style10 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

Practice

(1/5)
1.

What does the Global Interpreter Lock (GIL) in Ruby do?

easy
A. Automatically speeds up all multi-threaded Ruby programs
B. Allows multiple threads to run Ruby code simultaneously
C. Prevents any threads from running in Ruby
D. Allows only one thread to execute Ruby code at a time

Solution

  1. Step 1: Understand the role of GIL

    The GIL is a lock that ensures only one thread runs Ruby code at once.
  2. Final Answer:

    Allows only one thread to execute Ruby code at a time -> Option D
  3. Quick Check:

    GIL = single-thread execution [OK]
Hint: GIL means one thread runs Ruby code at a time [OK]
Common Mistakes:
  • Thinking GIL allows true parallel Ruby code execution
  • Believing GIL stops all threading
  • Assuming GIL speeds up multi-threading
2.

Which Ruby code snippet correctly starts two threads that print numbers?

Option A:
threads = [Thread.new { puts 1 }, Thread.new { puts 2 }]
threads.each(&:join)

Option B:
Thread.new { puts 1 }
Thread.new { puts 2 }

Option C:
threads = Thread.new { puts 1 }
threads = Thread.new { puts 2 }
threads.join

Option D:
Thread.start { puts 1 }
Thread.start { puts 2 }
easy
A. Creates two threads and waits for both to finish
B. Creates two threads but does not wait for them
C. Creates one thread twice, only waits for last
D. Uses undefined Thread.start method

Solution

  1. Step 1: Analyze thread creation and joining

    Creates two threads and waits for both to finish. Creates two threads and calls join on both, ensuring both finish.
  2. Final Answer:

    Creates two threads and waits for both to finish -> Option A
  3. Quick Check:

    Thread.new + join = wait for threads [OK]
Hint: Use join to wait for all threads to finish [OK]
Common Mistakes:
  • Not calling join and missing thread completion
  • Overwriting thread variables losing references
  • Using non-existent Thread.start method
3.

What will be the output of this Ruby code considering the GIL?

count = 0
threads = 5.times.map do
  Thread.new do
    1000.times { count += 1 }
  end
end
threads.each(&:join)
puts count
medium
A. 5000
B. 1000
C. May be less than 5000 due to race conditions
D. 0

Solution

  1. Step 1: Understand thread increments with GIL

    GIL allows one thread at a time, but count += 1 is not atomic and can cause race conditions.
  2. Final Answer:

    May be less than 5000 due to race conditions -> Option C
  3. Quick Check:

    Non-atomic increments + threads = race condition [OK]
Hint: GIL doesn't prevent race conditions on shared data [OK]
Common Mistakes:
  • Assuming GIL makes increments atomic
  • Expecting exact 5000 without synchronization
  • Ignoring thread interference on variables
4.

Identify the problem in this Ruby code using threads and fix it:

count = 0
threads = 3.times.map do
  Thread.new do
    100.times { count += 1 }
  end
end
threads.each(&:join)
puts count
medium
A. Add a Mutex to synchronize increments
B. Remove threads and run increments sequentially
C. Use Thread.kill to stop threads early
D. No problem, code is correct

Solution

  1. Step 1: Identify race condition on count variable

    Multiple threads increment count without synchronization causing race conditions.
  2. Final Answer:

    Add a Mutex to synchronize increments -> Option A
  3. Quick Check:

    Mutex prevents race conditions [OK]
Hint: Use Mutex to protect shared data in threads [OK]
Common Mistakes:
  • Ignoring race conditions on shared variables
  • Trying to kill threads instead of synchronizing
  • Assuming GIL handles all thread safety
5.

You want to speed up a CPU-heavy Ruby program using threads. Considering the GIL, what is the best approach?

hard
A. Disable the GIL in Ruby settings
B. Use multiple processes instead of threads
C. Use threads only for I/O tasks, not CPU tasks
D. Use more threads since GIL allows parallel CPU work

Solution

  1. Step 1: Understand GIL impact on CPU-bound threads

    GIL allows only one thread to run Ruby code at a time, so threads don't speed up CPU-heavy tasks.
  2. Final Answer:

    Use multiple processes instead of threads -> Option B
  3. Quick Check:

    Processes bypass GIL for CPU parallelism [OK]
Hint: Use processes, not threads, for CPU speedup in Ruby [OK]
Common Mistakes:
  • Believing threads run CPU tasks in parallel with GIL
  • Trying to disable GIL (not possible in standard Ruby)
  • Using threads for CPU tasks instead of I/O
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard