iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Quiz

Ruby track quiz: 10 questions covering core syntax, blocks, classes, modules, and the gotchas every Rubyist should know.

Ruby — track quiz

EXAMPLE
# 10 questions. Pass: >= 7. Answers + explanations at the bottom.

# ===== Q1 =====
# What does the following print?
puts ['a', 'b', 'c'].each_with_index.map { |x, i| "#{i}: #{x}" }
# A) 0: a 1: b 2: c (each on its own line)
# B) Error
# C) [0: a, 1: b, 2: c]
# D) An array of strings printed via Array#to_s

# ===== Q2 =====
# Which is truthy in Ruby?
# A) nil
# B) false
# C) 0
# D) None of the above

# ===== Q3 =====
# What is the difference between a Symbol :status and the string 'status'?
# A) None, they are interchangeable
# B) Symbols are immutable + interned; strings are mutable
# C) Symbols cannot be hash keys
# D) Symbols allocate fresh memory on each use

# ===== Q4 =====
# What is the output?
class Foo
  def initialize
    @x = 1
  end
end
puts Foo.new.instance_variable_get(:@y).inspect
# A) 'nil'
# B) Error
# C) 1
# D) NoMethodError

# ===== Q5 =====
# Which block-argument style is correct for a Hash#each?
{ a: 1, b: 2 }.each do |kv|
  puts kv
end
# A) Yes — kv is a 2-element array [key, value]
# B) No — must be |k, v|
# C) Both; Ruby destructures both ways
# D) Error

# ===== Q6 =====
# What does this print?
def make_counter
  count = 0
  -> { count += 1 }
end

c = make_counter
puts c.call
puts c.call
# A) 0, 1
# B) 1, 2
# C) Error (closure escapes scope)
# D) nil, nil

# ===== Q7 =====
# What does the safe-navigation operator do?
user&.profile&.bio
# A) Auto-creates missing intermediate objects
# B) Returns nil instead of raising NoMethodError when an intermediate is nil
# C) Throws if any intermediate is nil
# D) Same as user.try(:profile).try(:bio) — only works in Rails

# ===== Q8 =====
# What is the result?
['1', '2', '3'].map(&:to_i)
# A) [1, 2, 3]
# B) ['1', '2', '3']
# C) Error (no implicit conversion)
# D) [nil, nil, nil]

# ===== Q9 =====
# Which is NOT a valid way to define a class method?
class Foo
  def self.method1; end                       # A
  class << self
    def method2; end                          # B
  end
end
# C: def Foo.method3; end (outside the class body)
# D: All are valid

# ===== Q10 =====
# What is wrong with this code?
def add(a, b)
  return a + b
  puts 'done'
end
# A) Nothing
# B) The puts is unreachable; lint warns
# C) Ruby errors at runtime
# D) Requires explicit return statement

# ===== Answers =====
# 1. D — each_with_index.map returns array; puts prints with newlines
# 2. C — only nil and false are falsy in Ruby; 0 / '' / [] are truthy
# 3. B — Symbols are immutable interned values; strings are mutable
# 4. A — instance_variable_get returns nil for unset vars
# 5. C — Ruby destructures both the [k, v] array and |k, v| signature
# 6. B — closure captures count; each call increments
# 7. B — safe navigation returns nil on a nil receiver
# 8. A — Symbol#to_proc converts &:to_i to a block
# 9. D — all three styles work
# 10. B — puts after return is unreachable; most linters flag

# ===== Patterns to internalise =====
# - Only nil/false are falsy
# - Symbols for identifiers, strings for content
# - &:method shorthand uses Symbol#to_proc
# - instance_variable_get returns nil silently — careful!
# - Safe navigation (&.) for null-safe call chains

Why it matters

Quiz takeaway: master what is truthy (nil and false only), symbols vs strings, block destructuring, closures over scope, &:method shorthand, safe navigation, and the silence of instance_variable_get. The traps these questions cover are where most Ruby bugs hide.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# 3 questions per lesson.
Try it Yourself »

Discussion

Loading…