0
0
Rubyprogramming~20 mins

Method chaining patterns in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Method Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of chained string methods
What is the output of this Ruby code using method chaining?
Ruby
result = "  hello world  ".strip.upcase.reverse
puts result
A"DLROW OLLEH"
B"dlrow olleh"
C"HELLO WORLD"
D" HELLO WORLD "
Attempts:
2 left
💡 Hint
Think about what each method does in order: strip, upcase, then reverse.
Predict Output
intermediate
2:00remaining
Chained array transformations output
What does this Ruby code print?
Ruby
numbers = [1, 2, 3, 4, 5]
result = numbers.select(&:odd?).map { |n| n * 10 }.join(",")
puts result
A"2,4"
B"1,3,5"
C"10,30,50"
D"20,40"
Attempts:
2 left
💡 Hint
First select odd numbers, then multiply each by 10, then join with commas.
🔧 Debug
advanced
2:00remaining
Identify the error in method chaining
What error does this Ruby code raise?
Ruby
result = "123abc".to_i.to_s.reverse
puts result
ANoMethodError: undefined method `reverse' for Integer
BNo error, outputs "321"
CTypeError: can't convert String into Integer
DArgumentError: invalid value for Integer()
Attempts:
2 left
💡 Hint
Check the return types of each method in the chain.
🔧 Debug
advanced
2:00remaining
Which option raises a NoMethodError in method chaining?
Which of these Ruby code snippets will raise a NoMethodError?
A"HELLO".upcase!.reverse
B"hello".upcase.reverse
C"test".split.map(&:upcase)
D"example".reverse.upcase
Attempts:
2 left
💡 Hint
Bang methods like upcase! return nil if the string is not modified.
🚀 Application
expert
3:00remaining
Count words with method chaining
Using method chaining, which code correctly counts how many words in the string have length greater than 3?
Ruby
text = "Ruby is fun and powerful"
A
count = text.split.filter { |w| w.length > 3 }.size
puts count
B
count = text.split.map { |w| w.length > 3 }.count
puts count
C
count = text.split.reduce(0) { |acc, w| acc + (w.length > 3 ? 1 : 0) }
puts count
D
count = text.split.select { |w| w.length > 3 }.count
puts count
Attempts:
2 left
💡 Hint
Select words longer than 3 characters, then count them.