Challenge - 5 Problems
Ruby DSL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Look at how the DSL collects messages and joins them with a comma and space.
✗ Incorrect
The DSL collects the strings "Hello" and "world" in an array, then joins them with ", ". So the output is "Hello, world".
🧠 Conceptual
intermediate1: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?Attempts:
2 left
💡 Hint
Think about how DSL methods are called inside the block without specifying an object.
✗ Incorrect
Using instance_eval runs the block with self set to the DSL object, so methods defined on that object can be called directly inside the block.
🔧 Debug
advanced2: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"
endAttempts:
2 left
💡 Hint
Check how the block is called and what self is inside it.
✗ Incorrect
The block is called with block.call, which runs it in the current context (main), not the DSL object. So title is undefined there.
📝 Syntax
advanced2: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
endAttempts:
2 left
💡 Hint
Look at how method arguments are separated in Ruby.
✗ Incorrect
Ruby methods require commas between arguments. 'set :debug true' misses a comma, causing a syntax error.
🚀 Application
expert3: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?Attempts:
2 left
💡 Hint
Think about what happens when you add a key twice in a Ruby hash.
✗ Incorrect
Adding the same key twice overwrites the previous value. So keys :a and :b remain, total 2 keys.