Bird
Raised Fist0
Rubyprogramming~20 mins

Method lookup chain in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Method Lookup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Method lookup with modules and classes
What is the output of this Ruby code?
Ruby
module A
  def greet
    "Hello from A"
  end
end

module B
  def greet
    "Hello from B"
  end
end

class C
  include A
  include B
end

puts C.new.greet
AHello from B
BHello from A
CNoMethodError
DHello from C
Attempts:
2 left
💡 Hint
Remember that the last included module takes precedence in Ruby's method lookup chain.
🧠 Conceptual
intermediate
2:00remaining
Understanding method lookup order with inheritance and modules
Given a class that inherits from a superclass and includes a module, which order does Ruby follow to find a method?
A1, 2, 3, 4
B1, 3, 2, 4
C2, 1, 3, 4
D3, 4, 1, 2
Attempts:
2 left
💡 Hint
Think about where Ruby looks first: the class, then modules included in it, then the superclass and its modules.
❓ Predict Output
advanced
2:00remaining
Method lookup with prepend and include
What will this Ruby code print?
Ruby
module M
  def hello
    "Hello from M"
  end
end

class X
  include M
  def hello
    "Hello from X"
  end
end

class Y < X
  prepend M
end

puts Y.new.hello
AHello from Y
BHello from X
CHello from M
DNoMethodError
Attempts:
2 left
💡 Hint
Remember that prepend puts the module before the class in the lookup chain.
❓ Predict Output
advanced
2:00remaining
Method lookup with super and modules
What is the output of this Ruby code?
Ruby
module M
  def foo
    "foo from M"
  end
end

class A
  include M
end

class B < A
  def foo
    super + " and foo from B"
  end
end

obj = B.new
puts obj.foo
A" and foo from B"
B"foo from M"
CNoMethodError
D"foo from M and foo from B"
Attempts:
2 left
💡 Hint
Remember that super starts method lookup from the superclass and follows its full ancestor chain, including modules included there.
❓ Predict Output
expert
3:00remaining
Complex method lookup with multiple modules and prepend
What is the output of this Ruby code?
Ruby
module M1
  def call
    "M1"
  end
end

module M2
  def call
    "M2"
  end
end

class Base
  def call
    "Base"
  end
end

class Derived < Base
  include M1
  prepend M2
end

puts Derived.new.call
AM1
BM2
CBase
DNoMethodError
Attempts:
2 left
💡 Hint
Prepend modules come before the class in lookup order, even if the class includes other modules.

Practice

(1/5)
1.

What does the method lookup chain in Ruby represent?

easy
A. The list of all variables in a class
B. The order Ruby searches to find a method to execute
C. The sequence of method calls inside a method
D. The order Ruby compiles the code

Solution

  1. Step 1: Understand method lookup chain meaning

    The method lookup chain is the path Ruby follows to find a method when called on an object.
  2. Step 2: Compare options with definition

    Only The order Ruby searches to find a method to execute correctly describes this as the order Ruby searches for a method to execute.
  3. Final Answer:

    The order Ruby searches to find a method to execute -> Option B
  4. Quick Check:

    Method lookup chain = method search order [OK]
Hint: Think 'where Ruby looks for methods' [OK]
Common Mistakes:
  • Confusing method lookup with variable list
  • Thinking it shows method call sequence
  • Mixing lookup with code compilation order
2.

Which Ruby code correctly shows the method owner for to_s called on a string?

"hello".method(:to_s).owner
easy
A. String
B. Object
C. BasicObject
D. Kernel

Solution

  1. Step 1: Recall where to_s is defined for String

    The to_s method for String objects is actually defined in the Kernel module, which is included in Object, and thus available to String.
  2. Step 2: Check method owner for to_s on a String object

    Using "hello".method(:to_s).owner returns Kernel because String does not override to_s.
  3. Final Answer:

    Kernel -> Option D
  4. Quick Check:

    to_s owner for String = Kernel [OK]
Hint: String inherits to_s from Kernel [OK]
Common Mistakes:
  • Assuming method is owned by String
  • Confusing Object and BasicObject
  • Forgetting Kernel defines to_s
3.

Given this Ruby code, what is the output?

module M1
  def greet; "Hello from M1"; end
end

class C
  include M1
  def greet; "Hello from C"; end
end

obj = C.new
puts obj.greet
medium
A. Hello from C
B. Hello from M1
C. NoMethodError
D. Hello from Object

Solution

  1. Step 1: Understand method lookup order with include and override

    Class C includes module M1, but also defines its own greet method, which overrides the module's method.
  2. Step 2: Determine which greet method is called

    Ruby looks first in C, finds greet there, so it calls "Hello from C".
  3. Final Answer:

    Hello from C -> Option A
  4. Quick Check:

    Method override in class wins over included module [OK]
Hint: Class methods override included module methods [OK]
Common Mistakes:
  • Thinking included module method runs first
  • Expecting NoMethodError due to confusion
  • Assuming Object's method is called
4.

What is the error in this Ruby code snippet?

module M
  def hello; "Hi from M"; end
end

class A
  prepend M
  def hello; "Hi from A"; end
end

obj = A.new
puts obj.hello
medium
A. No error, output is "Hi from M"
B. No error, output is "Hi from A"
C. SyntaxError due to prepend usage
D. NoMethodError for hello

Solution

  1. Step 1: Understand prepend effect on method lookup

    Prepending module M places it before class A in the lookup chain, so M's methods override A's.
  2. Step 2: Determine which hello method runs

    Calling hello on obj calls M's method, outputting "Hi from M".
  3. Final Answer:

    No error, output is "Hi from M" -> Option A
  4. Quick Check:

    Prepend puts module before class in lookup [OK]
Hint: Prepend modules override class methods [OK]
Common Mistakes:
  • Expecting class method to run instead
  • Thinking prepend causes syntax error
  • Confusing prepend with include
5.

Consider this Ruby code:

module M1
  def info; "M1"; end
end

module M2
  def info; "M2"; end
end

class Base
  def info; "Base"; end
end

class Derived < Base
  include M1
  prepend M2
end

obj = Derived.new
puts obj.info

What will be printed and why?

hard
A. M1, because include puts M1 before Base
B. Base, because class method overrides modules
C. M2, because prepend puts M2 before Derived in lookup
D. NoMethodError, info not found

Solution

  1. Step 1: Analyze method lookup order with prepend and include

    Prepend M2 places it before Derived, then Derived, then included M1, then Base.
  2. Step 2: Identify which info method is called

    Since M2 is first in lookup, its info method runs, printing "M2".
  3. Final Answer:

    M2, because prepend puts M2 before Derived in lookup -> Option C
  4. Quick Check:

    Prepend > class > include > superclass [OK]
Hint: Prepend modules come before class, include after [OK]
Common Mistakes:
  • Thinking include module method runs first
  • Assuming class method overrides prepend module
  • Ignoring method lookup chain order
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