Project

General

Profile

Actions

Bug #11808

open

Different behavior between Enumerable#grep and Array#grep

Added by BenOlive (Ben Olive) almost 9 years ago. Updated 8 months ago.

Status:
Assigned
Target version:
-
[ruby-core:72067]
Tags:

Description

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

Here is an example that explains it better:

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


Related issues 1 (1 open0 closed)

Related to Ruby master - Bug #20807: String#gsub fails when called from string subclass with a block passedOpenActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0