Is it possible to make c1.say to show "lalala" without changing M1, M2, C1? Ie, use extra modules to override M2's method? Thanks,
module M1
def word
"lalala"
end
end
module M2
def word
super + 'wawawa'
end
end
class C1
include M1
include M2
def say
puts word
end
end
c1 = C1.new
c1.say # lalalawawawa
You can always monkey-patch any class without changing its original code.
module M1
def word
"lalala"
end
end
module M2
def word
super + 'wawawa'
end
end
class C1
include M1
include M2
def say
puts word
end
end
# patch M2
M2.class_eval do
def word
super
end
end
# or patch C1
# C1.class_eval do
# def word
# 'lalala'
# end
# end
c1 = C1.new
c1.say
# >> lalala
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.