From: Maurizio De Santis on 31 Jul 2010 21:19 Hello! suppose I want to modify Array.slice() (and consequentially, Array[]) in order to accept a RegExp as argument, and return values matching the regular expression. I tried to solve this problem in this way: class Array def slice_with_regexp(*args) puts args return self.select{ |val| val.to_s =~ args[0] } if args.size == 1 and args[0].is_a?(RegExp) end alias_method :slice_without_regexp, :slice alias_method :slice, :slice_with_regexp end arr = %w[a b c] puts arr.slice(/a/) but executing this script I get this error: (?-mix:a) scripts.rb:8:in `slice_with_regexp': uninitialized constant RegExp (NameError) from scripts.rb:17:in `<main>' Surely I did something wrong, but the very question is: what is the correct way to get the desired effect (that is:) arr = %w[a b c] puts arr[/a/] => ["a"] puts arr.slice(/a/) => ["a"] ? -- Posted via http://www.ruby-forum.com/.
From: James Harrison on 31 Jul 2010 22:24 > return self.select{ |val| val.to_s =~ args[0] } if args.size == 1 > and args[0].is_a?(RegExp) Try Regexp, not RegExp http://ruby-doc.org/core/classes/Regexp.html
From: Maurizio De Santis on 1 Aug 2010 05:02 James Harrison wrote: >> return self.select{ |val| val.to_s =~ args[0] } if args.size == 1 >> and args[0].is_a?(RegExp) > > Try Regexp, not RegExp > > http://ruby-doc.org/core/classes/Regexp.html LOL that's what I obtain when I'm still not asleep at 3.00 am :P But now it's morning, and I have still a problem; please consider this: class Array def slice_with_regexp(*args) return self.select{ |val| val =~ args[0] } if args.size == 1 and args[0].is_a?(Regexp) slice_without_regexp(*args) end alias_method :slice_without_regexp, :slice alias_method :slice_without_regexp, :[] alias_method :slice, :slice_with_regexp end arr = %w[a b c ab] puts arr.slice(/a/).inspect # => ["a", "ab"] slice with Regexp works; correct puts arr.slice(1).inspect # => "b" original slice still works; correct puts arr[/a/].inspect # => stack level too deep ??? why?? -- Posted via http://www.ruby-forum.com/.
From: Brian Candler on 1 Aug 2010 07:20 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. -- Posted via http://www.ruby-forum.com/.
From: David A. Black on 1 Aug 2010 07:57 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
|
Next
|
Last
Pages: 1 2 3 Prev: FREE SOFTWARE Next: Could I build a video chat site with ruby on rails? |