Bird
Raised Fist0
Rubyprogramming~5 mins

Extend for class methods in Ruby - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Extend for class methods
O(1)
Understanding Time Complexity

We want to understand how adding class methods using extend affects the time it takes to run code.

How does the program's work grow when we add methods this way?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

module Greetings
  def hello
    "Hello!"
  end
end

class Person
  extend Greetings
end

Person.hello

This code adds a class method hello to Person by extending it with the Greetings module.

Identify Repeating Operations

Look for loops or repeated steps in the code.

  • Primary operation: Adding methods to a class using extend happens once.
  • How many times: The extension runs only once when the class is defined or extended.
How Execution Grows With Input

Adding methods with extend does not depend on input size.

Input Size (n)Approx. Operations
10Constant steps to add methods
100Same constant steps
1000Still constant steps

Pattern observation: The work stays the same no matter how many times you call the method later.

Final Time Complexity

Time Complexity: O(1)

This means adding class methods with extend takes the same small amount of time regardless of input size.

Common Mistake

[X] Wrong: "Extending a class with many methods makes the program slower every time I call those methods."

[OK] Correct: The extension happens once when setting up the class, not each time you call the methods. Calling the methods later runs only their own code.

Interview Connect

Knowing how extend works helps you explain how Ruby adds class methods efficiently. This shows you understand how Ruby organizes code behind the scenes.

Self-Check

"What if we used include instead of extend? How would the time complexity change when adding methods?"

Practice

(1/5)
1. What does the extend keyword do in Ruby when used inside a class?
easy
A. Includes the module's constants only
B. Adds the module's methods as instance methods to the class
C. Adds the module's methods as class methods to the class
D. Removes methods from the class

Solution

  1. Step 1: Understand extend usage

    The extend keyword adds methods from a module as class methods to the class that calls it.
  2. Step 2: Differentiate from include

    Unlike include, which adds methods as instance methods, extend adds them as class methods.
  3. Final Answer:

    Adds the module's methods as class methods to the class -> Option C
  4. Quick 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
A. class MyClass; def self.extend MyModule; end
B. class MyClass; include MyModule; end
C. class MyClass; MyModule.extend(self); end
D. class MyClass; extend MyModule; end

Solution

  1. Step 1: Identify correct extend usage

    The correct syntax is to call extend inside the class body with the module name.
  2. Step 2: Check each option

    class MyClass; extend MyModule; end uses extend MyModule inside the class, which is correct syntax.
  3. Final Answer:

    class MyClass; extend MyModule; end -> Option D
  4. Quick Check:

    Correct extend syntax = 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.hello
medium
A. NoMethodError: undefined method 'hello' for Person
B. Hello from module
C. Hello from module called on instance
D. SyntaxError

Solution

  1. Step 1: Understand extend Greetings effect

    The module's method hello becomes a class method of Person.
  2. Step 2: Check method call

    Person.hello calls the class method, which exists and returns "Hello from module".
  3. Final Answer:

    Hello from module -> Option B
  4. Quick 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_name
medium
A. No error; output is 'Wrench'
B. NoMethodError because tool_name is undefined
C. SyntaxError due to extend usage
D. Output is 'Hammer' ignoring class method

Solution

  1. Step 1: Analyze method definitions

    The module Tools defines tool_name, which is added as a class method to Worker by extend Tools.
  2. Step 2: Check method override

    Worker defines its own class method tool_name, which overrides the module method.
  3. Final Answer:

    No error; output is 'Wrench' -> Option A
  4. Quick 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?
end
hard
A. Add extend InfoModule inside both Alpha and Beta classes
B. Add include InfoModule inside both Alpha and Beta classes
C. Define def self.info separately in Alpha and Beta
D. Use InfoModule.extend(self) outside classes

Solution

  1. Step 1: Understand sharing class methods

    Using extend InfoModule inside each class adds info as a class method.
  2. Step 2: Evaluate options

    Add extend InfoModule inside both Alpha and Beta classes correctly uses extend in both classes to share the method without repetition.
  3. Final Answer:

    Add extend InfoModule inside both Alpha and Beta classes -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard