加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有账号?注册会员
×
本帖最后由 LBQ 于 2013-9-26 21:29 编辑
- Monkeypatch v0.5 - LBQ的尝试 -
这个脚本的用途就如同他的名字一样——用来打猴子补丁
当然在下这种渣估计写出来的这个东西估计也会bug多多吧。。。
用法:
- class Scene_Base
- alias lbq_sb_start start
- def start
- lbq_sb_start
- puts "hi"
- end
- end
复制代码
等同于
- class Scene_Base
- monkeypatch :start do
- puts "hi"
- end
- end
复制代码
默认在最后加入代码
如果想要在之前加入的话用 monkeypatch :name, :before
于是这个理论上可以替代alias猴子补丁但是因为渣LBQ对Proc之类的东西了解不多=。=所以出错了麻烦说一下。。
- #===============================================================================
- # Monkeypatch v 0.5 by LBQ
- #===============================================================================
- module Monkeypatch
-
- $monkeypatch_blocks = {}
-
- def monkeypatch(*args,&block)
-
- pos = :none
- if args.length == 0
- raise
- elsif args.length == 1
- pos = :after
- elsif args.length == 2
- pos = args[1]
- else
- raise
- end
-
-
- method_name = args[0]
- old_name = method_name.to_sym
- new_name = "monkeypatch_#{self.inspect.downcase}_#{method_name.to_s}_lbq".to_sym
- alias_method new_name, old_name
-
- monkeypathcblockname = self.inspect.downcase+"_"+ method_name.to_s + rand(99999).to_s
- $monkeypatch_blocks[monkeypathcblockname.to_sym] = block
-
-
-
- if pos == :after
- class_eval %{
- def #{method_name}(*args)
- #{new_name.to_s}(*args)
- instance_eval(&$monkeypatch_blocks[:#{monkeypathcblockname}])
- end
- }
- else
- class_eval %{
- def #{method_name}(*args)
- instance_eval(&$monkeypatch_blocks[:#{monkeypathcblockname}])
- #{new_name.to_s}(*args)
- end
- }
- end
-
- end
- end
复制代码
突然发现貌似忘了给最后的附加代码了。。。
- class Module
- include Mokeypatch
- end
复制代码 |