Challenge - 5 Problems
Ruby Bang Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code using a bang method?
Consider the following Ruby code snippet. What will be printed?
Ruby
arr = [1, 2, 3] arr.reverse! puts arr.inspect
Attempts:
2 left
💡 Hint
Bang methods usually modify the object itself.
✗ Incorrect
The reverse! method reverses the array in place, changing arr to [3, 2, 1].
❓ Predict Output
intermediate2:00remaining
What does this string bang method return?
What will be the output of this Ruby code?
Ruby
str = "hello" result = str.upcase! puts result puts str
Attempts:
2 left
💡 Hint
upcase! changes the string itself and returns the modified string.
✗ Incorrect
upcase! converts str to uppercase in place and returns the modified string.
❓ Predict Output
advanced2:00remaining
What is the output when bang method does not change the object?
Look at this Ruby code. What will be printed?
Ruby
arr = [1, 2, 3] result = arr.sort! puts result.inspect puts arr.inspect
Attempts:
2 left
💡 Hint
Bang methods return nil if no changes were made.
✗ Incorrect
Since arr is already sorted, sort! returns nil and arr stays the same.
❓ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this Ruby code produce?
Ruby
num = 10
num.negate!Attempts:
2 left
💡 Hint
Check if Integer has a negate! method.
✗ Incorrect
Integer class does not have a negate! method, so NoMethodError is raised.
🧠 Conceptual
expert2:00remaining
Why do Ruby bang methods sometimes return nil?
In Ruby, some bang methods return nil instead of the modified object. Why does this happen?
Attempts:
2 left
💡 Hint
Think about what happens if the object is already in the desired state.
✗ Incorrect
Bang methods return nil when no changes are made to signal the object was unchanged.