From: Gabriel Saravia Date: 2009-05-07T18:11:33+09:00 Subject: Re: Adding a =~ method to the Method class Thank you for the quick response! To answer the other post about an example, The particular use I have for it was that I had an abstract class deriving from ActiveRecord. It made sense to override many of ActiveRecord's class methods: The AbstractClass methods are passed some params in addition to those standard to the ActiveRecord method, which it then uses those params to delegate onto the correct concrete sub class, to which it passes the standard ActiveRecord params. However, in the subclasses of this abstract class, I re-instate the ActiveRecord class methods. So, in order to test that I had actually reinstated the ActiveRecord methods, I needed to assert that my concrete sub class was using the ActiveRecord method and not the abstract one. so... !(MyAbstractClass.method(:find) == ActiveRecord::Base.method(:find)) but, MyConcreteSubclass.method(:find) =~ ActiveRecord::Base.method(:find) incidentally, since posting I attempted to go to bed, but couldn't, kept on thinking about the problem, when I remembered vaguely about unbound methods and came up with this purely ruby method solution: def test_class_methods_similar(klass1, klass2, method_name_symbol) unbound_method1 = klass1.method(method_name_symbol).unbind unbound_method2 = klass2.method(method_name_symbol).unbind unbound_method1 == unbound_method2 end The importance of the test truly came out when it helped me catch a bug - I had overwritten many methods, and had accidentally overwritten destroy with destroy_all. however, I like the posted solutions better for readability and the fact that it can be called directly from a class. and, did that post really come from matz? I ask because seeing as how I'm fairly new, (to Ruby, to Rails, etc.) and this is my first post ever to the Ruby forum, I'm feeling a slight sense of awe. just want to say, Ruby is a beautiful language. It has made me love programming after many of my college courses had pretty much destroyed any such thought. I must ask, any chance Method.=~ could make it into the Ruby codebase? I figure it must have a lot more uses whenever a solution involves overriding/passing around methods between classes, including most any use of alias_method? -Gabe KDr2 wrote: > *ruby-1.8.7-p160* > add to *eval.c,* after line *9323*: > > static VALUE > method_similar(method, other) > VALUE method, other; > { > struct METHOD *m1, *m2; > > if (TYPE(other) != T_DATA || RDATA(other)->dmark != > (RUBY_DATA_FUNC)bm_mark) > return Qfalse; > if (CLASS_OF(method) != CLASS_OF(other)) > return Qfalse; > > Data_Get_Struct(method, struct METHOD, m1); > Data_Get_Struct(other, struct METHOD, m2); > > if (m1->klass != m2->klass || m1->rklass != m2->rklass ||m1->body != > m2->body) > return Qfalse; > > return Qtrue; > } > > add to *eval.c* affter line *10046*: > > rb_define_method(rb_cMethod, "=~", method_similar, 1); -- Posted via http://www.ruby-forum.com/.