Recall & Review
beginner
What does the
extend keyword do in Ruby?It adds the methods from a module as class methods to the class or object it is called on.Click to reveal answer
beginner
How is
extend different from include in Ruby?<code>include</code> adds module methods as instance methods, while <code>extend</code> adds them as class methods.Click to reveal answer
beginner
Given <pre>module Greetings
def hello
"Hello!"
end
end
class Person
extend Greetings
end</pre> How do you call <code>hello</code>?You call it on the class:
Person.hello returns "Hello!".Click to reveal answer
intermediate
Can
extend be used on individual objects in Ruby?Yes, it adds the module's methods as singleton methods to that object only.
Click to reveal answer
intermediate
Why use <code>extend</code> instead of defining class methods directly?It promotes code reuse by sharing methods across classes without inheritance.
Click to reveal answer
What does
extend add to a Ruby class?✗ Incorrect
extend adds module methods as class methods.Which keyword adds module methods as instance methods in Ruby?
✗ Incorrect
include adds module methods as instance methods.If a module has method
foo, how do you call it after extend ModuleName in a class?✗ Incorrect
Methods added by
extend become class methods.Can
extend be used on a single object instance?✗ Incorrect
extend can add methods to a single object as singleton methods.What is a main benefit of using
extend for class methods?✗ Incorrect
extend helps reuse code by sharing methods across classes.Explain how
extend works to add class methods in Ruby.Think about where the methods become available after using extend.
You got /3 concepts.
Describe a scenario where using
extend is better than inheritance.Consider when you want to share behavior but classes don’t share a parent.
You got /3 concepts.