0
0
Rubyprogramming~20 mins

DSL building patterns in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby DSL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple Ruby DSL block
What is the output of this Ruby code that uses a simple DSL pattern?
Ruby
class GreetingDSL
  def initialize
    @messages = []
  end

  def say(text)
    @messages << text
  end

  def output
    @messages.join(", ")
  end
end

def greet(&block)
  dsl = GreetingDSL.new
  dsl.instance_eval(&block)
  dsl.output
end

puts greet do
  say "Hello"
  say "world"
end
AHello, world
BHello world
Csay Hello, say world
DError: undefined method 'say'
Attempts:
2 left
💡 Hint
Look at how the DSL collects messages and joins them with a comma and space.
🧠 Conceptual
intermediate
1:30remaining
Purpose of instance_eval in Ruby DSLs
In Ruby DSL building, what is the main purpose of using instance_eval inside a method that takes a block?
ATo execute the block in the context of the DSL object, allowing direct method calls without receiver
BTo run the block in a new thread for concurrency
CTo convert the block into a string for parsing
DTo prevent the block from accessing instance variables
Attempts:
2 left
💡 Hint
Think about how DSL methods are called inside the block without specifying an object.
🔧 Debug
advanced
2:30remaining
Why does this Ruby DSL code raise NoMethodError?
Given this Ruby DSL code, why does it raise NoMethodError when calling title inside the block?
class BookDSL
  def initialize
    @title = ""
  end

  def title(text)
    @title = text
  end
end

def book(&block)
  dsl = BookDSL.new
  block.call
  dsl
end

book do
  title "Ruby Guide"
end
AThe block is missing a parameter to receive the DSL object
BThe title method is private and cannot be called
CThe block is called without changing self, so title method is undefined in main context
DThe BookDSL class does not define a title method
Attempts:
2 left
💡 Hint
Check how the block is called and what self is inside it.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this Ruby DSL code
Which option correctly fixes the syntax error in this Ruby DSL snippet?
class ConfigDSL
  def set(key, value)
    @settings ||= {}
    @settings[key] = value
  end

  def settings
    @settings
  end
end

def config(&block)
  dsl = ConfigDSL.new
  dsl.instance_eval(&block)
  dsl.settings
end

config do
  set :mode, "production"
  set :debug true
end
ARemove the second set call entirely
BChange 'set :debug true' to 'set :debug, true'
CAdd parentheses: 'set(:debug true)'
DReplace 'set :debug true' with 'set :debug => true'
Attempts:
2 left
💡 Hint
Look at how method arguments are separated in Ruby.
🚀 Application
expert
3:00remaining
How many keys are in the resulting hash from this Ruby DSL?
Consider this Ruby DSL code that builds a hash:
class HashBuilder
  def initialize
    @hash = {}
  end

  def add(key, value)
    @hash[key] = value
  end

  def build
    @hash
  end
end

def build_hash(&block)
  builder = HashBuilder.new
  builder.instance_eval(&block)
  builder.build
end

result = build_hash do
  add :a, 1
  add :b, 2
  add :a, 3
end

puts result.size
How many keys does result have?
A3
B0
C1
D2
Attempts:
2 left
💡 Hint
Think about what happens when you add a key twice in a Ruby hash.