I have code like this:
class Gtk::Entry
def initialize(foo = '')
super()
call_a_custom_method_here(foo)
end
def call_a_custom_method_here
end
end
Gtk is ruby-gtk, the module Gtk.
I try to modify this existing widget, called Gtk::Entry.
I need to hook the initialize call to also call a custom method.
The above will trigger a warning though:
"method initialize redefined"
Is there a way to avoid the initialize-method redefined problem?
答案 0 :(得分:0)
Matz:
IIRC, warning was caused when you replace an existing method without making any alias to the original.
If you still need to call the original, alias it then chain it at the end of your new initialize
. If you don't, use remove_method
.
答案 1 :(得分:0)
我需要挂起初始化调用以调用自定义方法。
但这不是你在做什么。您覆盖 initialize
。它消失了。你没有挂钩。
有没有办法避免初始化方法重新定义的问题?
是。钩住方法,不要覆盖它。
module GtkEntryInitializeHook
def initialize(foo = '')
super()
call_a_custom_method_here(foo)
end
def call_a_custom_method_here
end
end
class Gtk::Entry
prepend GktEntryInitializeHook
end