0
0
Rubyprogramming~20 mins

Include for instance methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Include Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using include?

Consider the following Ruby code that uses include to add instance methods from a module to a class. What will be printed when the code runs?

Ruby
module Greetings
  def hello
    "Hello from module!"
  end
end

class Person
  include Greetings
end

p = Person.new
puts p.hello
ASyntaxError: unexpected keyword_end
BHello from module!
Cnil
DNoMethodError: undefined method `hello' for #<Person:0x000...>
Attempts:
2 left
💡 Hint

Remember that include adds module methods as instance methods to the class.

Predict Output
intermediate
2:00remaining
What happens if you call a module method without including it?

Look at this Ruby code where a module method is called on an instance of a class that does not include the module. What will happen?

Ruby
module Talk
  def speak
    "Speaking"
  end
end

class Animal
end

a = Animal.new
puts a.speak
ANoMethodError: undefined method `speak' for #<Animal:0x000...>
Bnil
CSyntaxError: unexpected end-of-input
DSpeaking
Attempts:
2 left
💡 Hint

Think about whether the class has access to the module's methods without including it.

🔧 Debug
advanced
2:00remaining
Why does this Ruby code raise an error?

Examine the Ruby code below. It tries to include a module for instance methods, but raises an error. What is the cause?

Ruby
module Friendly
  def greet
    "Hi!"
  end
end

class Robot
  include Friendly()
end

r = Robot.new
puts r.greet
ANameError because Friendly is undefined
BNoMethodError because greet is private
CNoMethodError: undefined method `()' for Friendly:Module
DWorks fine and prints "Hi!"
Attempts:
2 left
💡 Hint

Look closely at the include Friendly() statement.

🧠 Conceptual
advanced
2:00remaining
How does Ruby's include affect method lookup for instance methods?

In Ruby, when a module is included in a class, how does it affect the method lookup path for instance methods?

AThe module's methods override all class methods and become class methods themselves.
BThe module is ignored unless explicitly called with the module name.
CThe module is copied into the class as class methods only, not instance methods.
DThe module is inserted into the class's ancestor chain just above the class, so its instance methods are found before the class's own methods.
Attempts:
2 left
💡 Hint

Think about how Ruby searches for methods when you call them on an instance.

Predict Output
expert
3:00remaining
What is the output of this Ruby code with multiple includes and method overrides?

Analyze the following Ruby code where a class includes two modules with methods of the same name. What will be printed?

Ruby
module A
  def info
    "Module A"
  end
end

module B
  def info
    "Module B"
  end
end

class MyClass
  include A
  include B
  def info
    "MyClass"
  end
end

obj = MyClass.new
puts obj.info
AMyClass
BModule B
CModule A
DNoMethodError
Attempts:
2 left
💡 Hint

Remember that methods defined in the class override included module methods.