We use extend to add methods from a module directly to a class as class methods. This helps share behavior without repeating code.
Extend for class methods in Ruby
Introduction
Syntax
Ruby
module ModuleName def method_name # code end end class ClassName extend ModuleName end
extend adds module methods as class methods.
Use include to add methods as instance methods instead.
Examples
say_hello as a class method to Person.Ruby
module Greetings def say_hello "Hello from class!" end end class Person extend Greetings end puts Person.say_hello
square becomes a class method of Calculator.Ruby
module MathHelpers def square(x) x * x end end class Calculator extend MathHelpers end puts Calculator.square(5)
Sample Program
This program shows how log is used as a class method on Application by extending Logger.
Ruby
module Logger def log(message) "LOG: #{message}" end end class Application extend Logger end puts Application.log("Starting app")
Important Notes
Remember, extend adds methods to the class itself, not to instances.
You can extend multiple modules to add many class methods.
Using extend keeps your class methods organized and reusable.
Summary
extend adds module methods as class methods.
Use it to share behavior across classes without repeating code.
It helps keep instance and class methods separate and clear.
Practice
1. What does the
extend keyword do in Ruby when used inside a class?easy
Solution
Step 1: Understand
Theextendusageextendkeyword adds methods from a module as class methods to the class that calls it.Step 2: Differentiate from
Unlikeincludeinclude, which adds methods as instance methods,extendadds them as class methods.Final Answer:
Adds the module's methods as class methods to the class -> Option CQuick Check:
extend= class methods [OK]
Hint: Remember: extend = class methods, include = instance methods [OK]
Common Mistakes:
- Confusing extend with include
- Thinking extend adds instance methods
- Assuming extend removes methods
2. Which of the following is the correct syntax to add module methods as class methods using
extend?easy
Solution
Step 1: Identify correct
The correct syntax is to callextendusageextendinside the class body with the module name.Step 2: Check each option
class MyClass; extend MyModule; end usesextend MyModuleinside the class, which is correct syntax.Final Answer:
class MyClass; extend MyModule; end -> Option DQuick Check:
Correctextendsyntax = class MyClass; extend MyModule; end [OK]
Hint: Use 'extend ModuleName' inside class body for class methods [OK]
Common Mistakes:
- Using include instead of extend
- Calling extend on module instead of class
- Incorrect method definition syntax
3. What will be the output of the following Ruby code?
module Greetings
def hello
"Hello from module"
end
end
class Person
extend Greetings
end
puts Person.hellomedium
Solution
Step 1: Understand
The module's methodextend Greetingseffecthellobecomes a class method ofPerson.Step 2: Check method call
Person.hellocalls the class method, which exists and returns "Hello from module".Final Answer:
Hello from module -> Option BQuick Check:
Class method from extend = "Hello from module" [OK]
Hint: extend adds module methods as class methods, so Person.hello works [OK]
Common Mistakes:
- Thinking hello is an instance method
- Calling hello on an instance instead of class
- Expecting NoMethodError
4. Identify the error in this Ruby code snippet:
module Tools
def tool_name
"Hammer"
end
end
class Worker
extend Tools
def self.tool_name
"Wrench"
end
end
puts Worker.tool_namemedium
Solution
Step 1: Analyze method definitions
The module Tools definestool_name, which is added as a class method to Worker byextend Tools.Step 2: Check method override
Worker defines its own class methodtool_name, which overrides the module method.Final Answer:
No error; output is 'Wrench' -> Option AQuick Check:
Class method override works; output = 'Wrench' [OK]
Hint: Class methods override extended module methods if defined later [OK]
Common Mistakes:
- Assuming module method overrides class method
- Expecting NoMethodError
- Confusing instance and class methods
5. You want to share a method
info as a class method across multiple classes without repeating code. Which approach using extend is best?
module InfoModule
def info
"Shared class info"
end
end
class Alpha
# What to do here?
end
class Beta
# What to do here?
endhard
Solution
Step 1: Understand sharing class methods
Usingextend InfoModuleinside each class addsinfoas a class method.Step 2: Evaluate options
Addextend InfoModuleinside both Alpha and Beta classes correctly usesextendin both classes to share the method without repetition.Final Answer:
Add extend InfoModule inside both Alpha and Beta classes -> Option AQuick Check:
Use extend in each class to share class methods [OK]
Hint: Use extend inside each class to share class methods from a module [OK]
Common Mistakes:
- Using include instead of extend for class methods
- Defining methods separately instead of sharing
- Calling extend on module instead of class
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
