メソッドを入れ替える swap_memthods
を作りました
kiminonaha.rb
module Kernel
def swap_methods(a, b)
alias_method :__temp__, a
alias_method a, b
alias_method b, :__temp__
remove_method :__temp__
end
end
class Kiminonaha
def taki
puts "もしかして俺たち..."
end
def mitsuha
puts "もしかして私たち..."
end
def say
taki
mitsuha
puts "「「入れ替わってる~!?!?!?」」"
end
end
Kiminonaha.new.say
普通に実行
もしかして俺たち...
もしかして私たち...
「「入れ替わってる~!?!?!?」」
swap_methods
を追加して実行
def say
taki
mitsuha
puts "「「入れ替わってる~!?!?!?」」"
end
+ swap_methods :taki, :mitsuha
end
もしかして私たち...
もしかして俺たち...
「「入れ替わってる~!?!?!?」」
「入れ替わってる〜 」
使い所は無いです。