Bird
Raised Fist0
Rubyprogramming~20 mins

Method chaining patterns in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of method chaining in Ruby?
easy
A. To call multiple methods on the same object in one line
B. To create new objects automatically
C. To run methods in parallel threads
D. To convert objects into strings

Solution

  1. Step 1: Understand method chaining concept

    Method chaining allows calling several methods one after another on the same object without repeating the object name.
  2. Step 2: Identify the main purpose

    The main goal is to write shorter, cleaner code by linking method calls in one line.
  3. Final Answer:

    To call multiple methods on the same object in one line -> Option A
  4. Quick Check:

    Method chaining = calling multiple methods on one object [OK]
Hint: Method chaining links calls on one object in one line [OK]
Common Mistakes:
  • Thinking it creates new objects automatically
  • Confusing with threading or parallel execution
  • Believing it converts objects to strings
2. Which of the following is the correct syntax for method chaining in Ruby?
easy
A. object|method1|method2|method3
B. object.method1.method2.method3
C. object::method1::method2::method3
D. object->method1->method2->method3

Solution

  1. Step 1: Recall Ruby method call syntax

    In Ruby, methods are called on objects using dot notation: object.method.
  2. Step 2: Identify chaining syntax

    Method chaining uses multiple dots to call methods one after another on the same object.
  3. Final Answer:

    object.method1.method2.method3 -> Option B
  4. Quick Check:

    Ruby chaining uses dots between methods [OK]
Hint: Use dots (.) to chain methods in Ruby [OK]
Common Mistakes:
  • Using arrows (->) which is not Ruby syntax
  • Using double colons (::) which are for constants or modules
  • Using pipes (|) which are not method call operators
3. What will be the output of the following Ruby code?
class Box
  def initialize(value)
    @value = value
  end

  def add(x)
    @value += x
    self
  end

  def multiply(x)
    @value *= x
    self
  end

  def value
    @value
  end
end

box = Box.new(2)
result = box.add(3).multiply(4).value
puts result
medium
A. 20
B. 14
C. 10
D. 8

Solution

  1. Step 1: Trace the add method

    Start with @value = 2. add(3) adds 3: 2 + 3 = 5, returns self.
  2. Step 2: Trace the multiply method

    multiply(4) multiplies @value by 4: 5 * 4 = 20, returns self.
  3. Step 3: Call value method

    value returns @value which is 20.
  4. Final Answer:

    20 -> Option A
  5. Quick Check:

    2 + 3 = 5; 5 * 4 = 20 [OK]
Hint: Follow each method step-by-step, remember self returns object [OK]
Common Mistakes:
  • Forgetting that add and multiply return self
  • Calculating operations in wrong order
  • Returning intermediate values instead of final
4. Identify the error in the following Ruby code that tries to chain methods:
class Person
  def initialize(name)
    @name = name
  end

  def greet
    puts "Hello, #{@name}!"
  end

  def farewell
    puts "Goodbye, #{@name}!"
  end
end

p = Person.new("Sam")
p.greet.farewell
medium
A. initialize method missing return statement
B. farewell method is misspelled
C. Cannot create Person object with new
D. greet method does not return self, so chaining fails

Solution

  1. Step 1: Check method return values

    greet prints a message but returns nil (default for puts).
  2. Step 2: Understand chaining requirement

    For chaining, greet must return self to allow calling farewell next.
  3. Final Answer:

    greet method does not return self, so chaining fails -> Option D
  4. Quick Check:

    Method chaining needs methods to return self [OK]
Hint: Ensure methods return self to chain calls [OK]
Common Mistakes:
  • Assuming puts returns self
  • Thinking initialize needs explicit return
  • Believing object creation is the problem
5. You want to create a Ruby class TextFormatter that allows chaining methods to transform a string. The methods are:
- upcase: converts text to uppercase
- reverse: reverses the text
- append(str): adds str to the end

Which implementation correctly supports chaining all these methods and returns the final string with result?
hard
A. Each method prints the modified string and returns nil
B. Each method returns the modified string directly without returning self
C. Each method modifies @text, returns self; add a result method returning @text
D. Each method returns a new TextFormatter object with modified text

Solution

  1. Step 1: Understand chaining requirements

    To chain methods, each must return self (the object) so next method can be called.
  2. Step 2: Provide final output method

    Since methods return self, a separate method (like result) should return the final string.
  3. Final Answer:

    Each method modifies @text, returns self; add a result method returning @text -> Option C
  4. Quick Check:

    Return self to chain, separate method to get final string [OK]
Hint: Return self in methods, separate method returns final value [OK]
Common Mistakes:
  • Returning strings breaks chaining
  • Printing inside methods returns nil
  • Returning new objects breaks original chain
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard