0
0
Rubyprogramming~20 mins

Extend for class methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Extend Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of extending a module for class methods
What is the output of this Ruby code when the class method greet is called?
Ruby
module Friendly
  def greet
    "Hello from module"
  end
end

class Person
  extend Friendly
end

puts Person.greet
A"Hello from module"
BSyntaxError: unexpected keyword_end
CNoMethodError: undefined method `greet' for Person:Class
D"Hello from instance"
Attempts:
2 left
💡 Hint
Remember that extend adds module methods as class methods.
Predict Output
intermediate
2:00remaining
Effect of including vs extending a module
What will be the output of this Ruby code?
Ruby
module Talk
  def speak
    "Speaking"
  end
end

class Animal
  include Talk
end

class Robot
  extend Talk
end

puts Animal.new.speak
puts Robot.speak
A
NoMethodError: undefined method `speak' for Animal:Class
Speaking
BNoMethodError for both calls
C
Speaking
Speaking
D
Speaking
NoMethodError: undefined method `speak' for Robot:Class
Attempts:
2 left
💡 Hint
Including adds instance methods; extending adds class methods.
🔧 Debug
advanced
2:00remaining
Why does this class method call fail?
Given this Ruby code, why does calling Car.honk raise an error?
Ruby
module Sounds
  def honk
    "Beep beep!"
  end
end

class Car
  include Sounds
end

puts Car.honk
ABecause the module method <code>honk</code> is private and cannot be called.
BBecause <code>include</code> adds instance methods, not class methods, so <code>Car.honk</code> is undefined.
CBecause <code>Car</code> forgot to inherit from <code>Sounds</code>.
DBecause <code>honk</code> is defined with wrong syntax inside the module.
Attempts:
2 left
💡 Hint
Think about the difference between include and extend.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in extending a module
Which option contains a syntax error when trying to add class methods using a module?
Ruby
module Greetings
  def hello
    "Hi!"
  end
end

class User
  extend Greetings
end
A
class User
  extend Greetings.new
end
B
class User
  extend(Greetings)
end
C
class User
  extend Greetings()
end
D
class User
  extend Greetings
end
Attempts:
2 left
💡 Hint
Remember how to use extend with modules.
🚀 Application
expert
3:00remaining
How to add both instance and class methods from a module
You want to add instance methods and class methods from a single module Features to a class Gadget. Which code correctly achieves this?
Ruby
module Features
  def instance_feature
    "Instance method"
  end

  module ClassMethods
    def class_feature
      "Class method"
    end
  end
end

class Gadget
  # What goes here?
end

puts Gadget.new.instance_feature
puts Gadget.class_feature
A
class Gadget
  extend Features
  extend Features::ClassMethods
end
B
class Gadget
  extend Features
  include Features::ClassMethods
end
C
class Gadget
  include Features
  include Features::ClassMethods
end
D
class Gadget
  include Features
  extend Features::ClassMethods
end
Attempts:
2 left
💡 Hint
Use include for instance methods and extend for class methods.