module M
def self.included(base)
puts 'this will be printed when this module will be included inside a class'
base.extend ClassMethods
base.class_eval do
scope :disabled, -> { where(disabled: true) }
end
end
def print_object_class
self.class.name
end
module ClassMethods
def print_class_name
self.name
end
end
end
module, , ()
self.included - , , .
declare module ClassMethods ,
, module ClassMethods, ,
Ex , Product,
class Product < ActiveRecord::Base
include M
puts 'after including M'
end
rails, , M class Product,
this will be printed when this module will be included inside a class
after including M .
Product.disabled
Product.print_class_name
Product.new.print_object_class
, M , .
module N
def self.abc
puts 'basic module'
end
end
abc define
N.abc # outputs 'basic module'
class Product < ActiveRecord::Base
include N
end
Product.ab# , Product
Product.new.ab# , Product
, .
, , .