木村です。
Regexp クラスのcompileメソッドを再定義しようとして
class Regexp
alias orig_compile compile
end
とすると、
NameError: undefined method `compile' for class `Regexp'
となります(ruby 1.9.0 2006-01-05 mswin32)。
compile のほかにも escape, union, last_match, quote
が同様のエラーとなります。これらの共通点を調べてみると、
rb_define_singleton_method(rb_cRegexp, "compile", rb_class_new_instance, -1);
rb_define_singleton_method(rb_cRegexp, "quote", rb_reg_s_quote, -1);
rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, -1);
rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union, -1);
rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
のように rb_define_singleton_method で定義されたメソッドなのですが、
他のクラスで同様に定義されているメソッドではエラーにならないものも
あります(Fileクラスの atとか)。
エラーになる/ならないの境界はどこにあるのでしょうか?
また、エラーになるのが正しいとしても、undefined method というのは
適切ではないと思いますがいかがでしょうか。
--
木村浩一