What if you could add powerful class methods to many classes with just one simple step?
Why Extend for class methods in Ruby? - Purpose & Use Cases
Imagine you have a group of related methods that you want to use directly on a class, but you try to add them one by one to each class manually.
For example, you want to add some utility methods to several classes, but you have to copy and paste the same code into each class.
This manual way is slow and boring because you repeat the same code many times.
It is easy to make mistakes or forget to update all copies when you change something.
Also, it makes your code messy and hard to maintain.
Using extend lets you add a whole set of methods to a class at once.
You write the methods once in a module, then extend that module in any class to get those methods as class methods.
This keeps your code clean, DRY (Don't Repeat Yourself), and easy to update.
class MyClass def self.greet puts 'Hello!' end end class AnotherClass def self.greet puts 'Hello!' end end
module Greeter def greet puts 'Hello!' end end class MyClass extend Greeter end class AnotherClass extend Greeter end
You can share useful class methods easily across many classes without repeating code.
Suppose you have a logging method you want all your classes to use as a class method. You put it in a module and extend that module in each class to keep logging consistent everywhere.
Manual copying of class methods is slow and error-prone.
extend adds module methods as class methods cleanly.
This keeps code DRY and easy to maintain.
Practice
extend keyword do in Ruby when used inside a class?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]
- Confusing extend with include
- Thinking extend adds instance methods
- Assuming extend removes methods
extend?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]
- Using include instead of extend
- Calling extend on module instead of class
- Incorrect method definition syntax
module Greetings
def hello
"Hello from module"
end
end
class Person
extend Greetings
end
puts Person.helloSolution
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]
- Thinking hello is an instance method
- Calling hello on an instance instead of class
- Expecting NoMethodError
module Tools
def tool_name
"Hammer"
end
end
class Worker
extend Tools
def self.tool_name
"Wrench"
end
end
puts Worker.tool_nameSolution
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]
- Assuming module method overrides class method
- Expecting NoMethodError
- Confusing instance and class methods
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?
endSolution
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]
- Using include instead of extend for class methods
- Defining methods separately instead of sharing
- Calling extend on module instead of class
Ready to prove it?
Pass the test and this pattern moves unknown
