Bird
Raised Fist0
Rubyprogramming~10 mins

Method lookup chain in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the method defined in the class.

Ruby
class Animal
  def speak
    'Hello'
  end
end

animal = Animal.new
puts animal.[1]
Drag options to blanks, or click blank then click option'
Aspeak
Brun
Cjump
Dtalk
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method that does not exist in the class.
Using a wrong method name.
2fill in blank
medium

Complete the code to call the method from the module included in the class.

Ruby
module Talk
  def speak
    'Hi from module'
  end
end

class Person
  include Talk
end

p = Person.new
puts p.[1]
Drag options to blanks, or click blank then click option'
Arun
Bspeak
Cjump
Dtalk
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method not defined in the module or class.
Confusing module methods with class methods.
3fill in blank
hard

Fix the error by completing the code to call the method from the superclass.

Ruby
class Vehicle
  def start
    'Starting engine'
  end
end

class Car < Vehicle
end

car = Car.new
puts car.[1]
Drag options to blanks, or click blank then click option'
Adrive
Bpark
Cstop
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method not defined in the class or its ancestors.
Using a method name that does not exist.
4fill in blank
hard

Fill both blanks to complete the method lookup chain with module and superclass methods.

Ruby
module M
  def greet
    'Hello from M'
  end
end

class A
  def greet
    'Hello from A'
  end
end

class B < A
  include M
end

b = B.new
puts b.[1]
puts b.class.superclass.instance_method(:[2]).bind(b).call if b.class.superclass.method_defined?(:[2])
Drag options to blanks, or click blank then click option'
Agreet
Bstart
Crun
Dspeak
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names for the two blanks.
Not matching the method name in the superclass call.
5fill in blank
hard

Fill all three blanks to complete the method lookup chain with class, module, and superclass methods.

Ruby
module M1
  def action
    'Action from M1'
  end
end

module M2
  def action
    'Action from M2'
  end
end

class Parent
  def action
    'Action from Parent'
  end
end

class Child < Parent
  include M1
  include M2

  def action
    'Action from Child'
  end
end

c = Child.new
puts c.[1]
puts c.singleton_class.ancestors[2].instance_method(:action).bind(c).call
puts c.class.superclass.instance_method(:[2]).bind(c).call if c.class.superclass.method_defined?(:[3])
Drag options to blanks, or click blank then click option'
Aaction
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names for blanks.
Confusing the order of method lookup.

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