Ruby On Rails 'Include' and 'Extend' example
Module level class method can't be available to any class use 'Include' Or 'Extend', Only modules instance methods available
When use include keyword for include a module into class it make module level instance method as to class level instance method
When use extend keyword for extend a module into class it make module level instance method as to class level class method
module Sample
def ins_mth; end
def self.cls_mth; end
end
class S1
include Sample
end
class S2
extend Sample
end
s1 = S1.new
s2 = S2.new
begin
S1.ins_mth
p 'Included modules instance method available as class Method'
rescue Exception => e
begin
s1.ins_mth
p 'Included modules instance method available as instance Method'
rescue Exception => e
p 'Included modules instance method does not available anywhere'
end
end
begin
S1.cls_mth
p 'Included modules class method available as class Method'
rescue Exception => e
begin
s1.cls_mth
p 'Included modules class method available as instance Method'
rescue Exception => e
p 'Included modules class method does not available anywhere'
end
end
begin
S2.ins_mth
p 'Extended modules instance method available as class Method'
rescue Exception => e
begin
s2.ins_mth
p 'Extended modules instance method available as instance Method'
rescue Exception => e
p 'Extended modules instance method does not available anywhere'
end
end
begin
S2.cls_mth
p 'Extended modules class method available as class Method'
rescue Exception => e
begin
s2.cls_mth
p 'Extended modules class method available as instance Method'
rescue Exception => e
p 'Extended modules class method does not available anywhere'
end
end
Out put of this code :
"Included modules instance method available as instance Method"
"Included modules class method does not available anywhere"
"Extended modules instance method available as class Method"
"Extended modules class method does not available anywhere"
Example code:
module Sample
def ins_mth; end
def self.cls_mth; end
end
class S1
include Sample
end
class S2
extend Sample
end
s1 = S1.new
s2 = S2.new
begin
S1.ins_mth
p 'Included modules instance method available as class Method'
rescue Exception => e
begin
s1.ins_mth
p 'Included modules instance method available as instance Method'
rescue Exception => e
p 'Included modules instance method does not available anywhere'
end
end
begin
S1.cls_mth
p 'Included modules class method available as class Method'
rescue Exception => e
begin
s1.cls_mth
p 'Included modules class method available as instance Method'
rescue Exception => e
p 'Included modules class method does not available anywhere'
end
end
begin
S2.ins_mth
p 'Extended modules instance method available as class Method'
rescue Exception => e
begin
s2.ins_mth
p 'Extended modules instance method available as instance Method'
rescue Exception => e
p 'Extended modules instance method does not available anywhere'
end
end
begin
S2.cls_mth
p 'Extended modules class method available as class Method'
rescue Exception => e
begin
s2.cls_mth
p 'Extended modules class method available as instance Method'
rescue Exception => e
p 'Extended modules class method does not available anywhere'
end
end
Out put of this code :
"Included modules instance method available as instance Method"
"Included modules class method does not available anywhere"
"Extended modules instance method available as class Method"
"Extended modules class method does not available anywhere"
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment