public
Created

Including vs Extending a Module - Updates with module example

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# Here, I am define a "module" with name Ma. I also define two methods. One without "self."
# and one with. See, later on, what happens when I "include" and what happens when I "extend"
# the "module" within a "class".
#
module Ma
# I will be able to make this method an instance or a class method of a class.
# It depends whether I will "include" or "extend" the module in the class.
# Note that this method, I cannot call it directly on Ma. In order for this method
# to be useful, I have to include or extend this module within a class.
#
def ma_method1
puts "ma_method1"
end
 
# This method, is not reusable, in the sense that I cannot make it be an instance of class
# method of a class. But still, it is a method of module Ma and I can call it directly.
#
def self.ma_method2
puts "ma_method2"
end
end
 
puts "Ma responds to ma_method1? : #{Ma.respond_to?(:ma_method1)}" # it will print "false"
puts "Ma responds to ma_method2? : #{Ma.respond_to?(:ma_method2)}" # it will print "true"
puts "-------------"
 
class A
# "include" sets the module methods as instance methods of the class
include Ma
end
 
a = A.new
puts "a Responds to ma_method1?: #{a.respond_to?(:ma_method1)}" # it will print "true"
puts "A Responds to ma_method1?: #{A.respond_to?(:ma_method1)}" # it will print "false"
puts "a Responds to ma_method2?: #{a.respond_to?(:ma_method2)}" # it will print "false"
puts "A Responds to ma_method2?: #{A.respond_to?(:ma_method2)}" # it will print "false"
puts "-------------"
 
class B
# "extend" sets the module methods as class methods of the class
extend Ma
end
 
b = B.new
puts "b Responds to ma_method1? : #{b.respond_to?(:ma_method1)}" # it will print "false"
puts "B Responds to ma_method1? : #{B.respond_to?(:ma_method1)}" # it will print "true"
puts "b responds to ma_method2? : #{b.respond_to?(:ma_method2)}" # it will print "false"
puts "B responds to ma_method2? : #{B.respond_to?(:ma_method2)}" # it will print "false"
puts "-------------------"
 
 
module C
include Ma
end
 
puts "C Responds to ma_method1? : #{C.respond_to?(:ma_method1)}" # it will print "false"
puts "C responds to ma_method2? : #{C.respond_to?(:ma_method2)}" # it will print "false"
puts "-------------------"
 
module D
extend Ma
end
 
puts "D Responds to ma_method1? : #{D.respond_to?(:ma_method1)}" # it will print "true"
puts "D responds to ma_method2? : #{D.respond_to?(:ma_method2)}" # it will print "false"
puts "-------------------"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.