Reputation: 11904
module A
end
class Klass
include A
end
How does this include influence Klass? Does it simply put Klass into module A or do something more?
Upvotes: 0
Views: 982
Reputation: 11107
Short Answer: If you have some methods inside your module and you use include
in a class, those methods can be used in the class.
Module A
def shout
puts "HEY THERE!!!!"
end
end
class Klass
include A
end
# Create instance of Klass
instance = Klass.new
# Produces "HEY THERE!!!!"
instance.shout
Upvotes: 1
Reputation: 79552
include
is one of the ways to include methods of a Module in another Module or Class.
Please read my article on how that affects method calls in Ruby/
Upvotes: 1
Reputation: 4337
The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins). For example, if your class defines the method "each", you can include the mixin module Enumerable and it can act as a collection. This can be confusing as the include verb is used very differently in other languages.
from here: What is the difference between include and require in Ruby?
also take a look at this page: http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html it has a verbose explanation about how include works.
Upvotes: 1