Please explain why self is used in def self.included (klass) below.
module A
def self.included(klass)
puts "A -> #{klass}"
puts A
puts self
end
end
class X
include A
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By writing
def self.includedyou are defining a method that is part of the singleton class of module A. In general, singleton methods can only be called by doingA.included()but this singleton method has a special nameincludedthat causes the Ruby interpreter to call when the module gets included in to a class.A normal method in a module (defined with
def foo) can only be called if the module gets included in to something else.