Project

General

Profile

Bug #11808

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

Regex special global variables are available within the block for `Array#grep`, Array#grep, but are nil within the block for `Enumerable#grep`. Enumerable#grep. 

 Here is an example that explains it better: 

 ~~~ruby ~~~ 
 class Test 
   include Enumerable 
   def each 
     return enum_for(:each) unless block_given? 
     yield "Hello" 
     yield "World" 
   end 
 end 

 enum = Test.new 
 array = ["Hello", "World"] 

 enum.grep(/^(.)/) {$1} # => [nil, nil] 
 array.grep(/^(.)/) {$1} # => ["H", "W"] 
 ~~~ 

 Tested on 2.0.0, 2.1.5, & 2.2.2

Back