Challenge - 5 Problems
Ruby Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of string interpolation with variables
What is the output of this Ruby code?
Ruby
name = "Alice" age = 30 puts "My name is #{name} and I am #{age} years old."
Attempts:
2 left
💡 Hint
Remember that #{variable} inside double quotes evaluates the variable's value.
✗ Incorrect
In Ruby, string interpolation with #{variable} inside double quotes replaces the expression with the variable's value.
❓ Predict Output
intermediate2:00remaining
Interpolation with expressions inside #{}
What will this Ruby code print?
Ruby
a = 5 b = 3 puts "Sum is #{a + b} and product is #{a * b}."
Attempts:
2 left
💡 Hint
Inside #{}, Ruby evaluates the expression before converting to string.
✗ Incorrect
Ruby evaluates expressions inside #{}, so a + b becomes 8 and a * b becomes 15.
🔧 Debug
advanced2:00remaining
Identify the error in string interpolation
What error does this Ruby code produce?
Ruby
name = "Bob" puts 'Hello, #{name}!'
Attempts:
2 left
💡 Hint
Check the type of quotes used for string interpolation.
✗ Incorrect
Single quotes in Ruby do not process #{}, so the output is the literal string with #{name}.
❓ Predict Output
advanced2:00remaining
Complex interpolation with method call
What is the output of this Ruby code?
Ruby
def greet(name) "Hello, #{name.upcase}!" end puts greet("eve")
Attempts:
2 left
💡 Hint
Inside #{}, you can call methods on variables.
✗ Incorrect
The method upcase converts the string to uppercase before interpolation.
🧠 Conceptual
expert3:00remaining
Why does this interpolation fail?
Consider this Ruby code:
```ruby
number = 10
puts "Value: #{number + '5'}"
```
What error will this code raise and why?
Attempts:
2 left
💡 Hint
Check the types of operands inside the interpolation expression.
✗ Incorrect
Ruby cannot add an Integer (number) and a String ('5'), causing a TypeError.