Array#grep(/pat/){...} allows me to get $1, $2, ... in block argument,
but Enumerable#grep(/pat/){...} doesn't.
Example code:
01: class ArrayWrapper
02: include Enumerable
03: def initialize(arr)
04: @arr = arr
05: end
06: def each(&block)
07: @arr.each(&block)
08: end
09: #def grep(pat, &block)
10: # @arr.grep(pat, &block)
11: #end
12: end
13:
14: arr = ["A1.png", "B1.jpg", "C1.gif"]
15: p arr.grep(/(\.png)$/) { $1 } #=> [".png"]
16:
17: obj = ArrayWrapper.new(arr)
18: p obj.grep(/(\.png)$/) { $1 } #=> [nil]
At line 15, $1 in block argument holds matched value.
(This behavior is what I expected.)
But at line 18, $1 is nil.
What is wrong at line 18?
···
--
kwatch