The quick and dirty. Including a Ruby Module is for instance methods and extending is for inheriting class variables in your classes.

1) Here is include

module Pizza
  def self.included(base)
    base.extend(ClassMethods)
  end
 
  module ClassMethods
    def style
      puts 'wood fired'
    end
  end
 
  def arugula
    puts 'true'
  end
end
 
class Restaurant
  include Pizza
end
 
# Calling a class method
Restaurant.style 
=> wood fired
 
# Calling an instance
Restaurant.new.arugula
=> true 
 
# Calling a instance as a class
Restaurant.arugula # NoMethodError: undefined method ‘arugula’ for Restaurant:Class
 
# Calling a class method as an instance
Restaurant.new.style # NoMethodError: undefined method ‘style’

2) Here is extend

 
class Restaurant
  extend Pizza
end
 
# Calling the same instance as a class method
Restaurant.arugula
=> true
 
# Calling the instance now doesn't work
Restaurant.new.arugula  # NoMethodError: undefined method 'arugula'

:) Enjoy your pizza

Leave a Reply

Your email address will not be published. Required fields are marked *