From: "David A. Black" Date: 2010-08-01T20:57:20+09:00 Subject: Re: what is the correct way to extend native methods? Hi -- On Sun, 1 Aug 2010, Brian Candler wrote: > Maurizio De Santis wrote: >> why?? > > What ruby are you using? With ruby 1.8.7 (2010-01-10 patchlevel 249) > [x86_64-linux] and your posted code, I get the following output: > > ["a", "ab"] > "b" > ert.rb:17:in `[]': can't convert Regexp into Integer (TypeError) > from ert.rb:17 > > If I change it to > > alias_method :slice_without_regexp, :slice > alias_method :slice, :slice_with_regexp > alias_method :[], :slice_with_regexp > > then *that* gives a stack too deep error, but on the initial slice call. > > I'm afraid I don't know the solution though. I suspect slice is already > an alias for [], or vice versa. They are indeed the same method: rb_define_method(rb_cArray, "[]", rb_ary_aref, -1); rb_define_method(rb_cArray, "slice", rb_ary_aref, -1); David -- David A. Black, Senior Developer, Cyrus Innovation Inc. The Ruby training with Black/Brown/McAnally Compleat Philadelphia, PA, October 1-2, 2010 Rubyist http://www.compleatrubyist.com