Project

General

Profile

Bug #11808

Different behavior between Enumerable#grep and Array#grep

Added by BenOlive (Ben Olive) almost 4 years ago. Updated 3 months ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:72067]

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

History

Updated by jeremyevans0 (Jeremy Evans) 4 months ago

Array#grep is actually Enumerable#grep:

Array.instance_method(:grep).owner
=> Enumerable

If I had to guess, the cause of the difference is that Array#each is implemented in C, and Test#each is implemented in Ruby, and this affects Regexp special variable scope. You see similar behavior as Array in other classes that implement #each in C, such as Range or File.

The documentation for the special global variables states: These global variables are thread-local and method-local variables. This indicates to me that the bug is that the variables are accessible inside the Array#each block, since that block executes inside the current method, it's not local to the Array#each method. However, I would assume removing the current behavior would break too much existing code.

Updated by nobu (Nobuyoshi Nakada) 4 months ago

  • Backport deleted (2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN)
  • ruby -v deleted (2.2.2)
  • Description updated (diff)
  • Tracker changed from Bug to Feature
#3

Updated by nobu (Nobuyoshi Nakada) 4 months ago

  • Subject changed from DIfferent behavior between Enumerable#grep and Array#grep to Different behavior between Enumerable#grep and Array#grep

Updated by matz (Yukihiro Matsumoto) 3 months ago

  • Backport set to 2.5: UNKNOWN, 2.6: UNKNOWN
  • Tracker changed from Feature to Bug

It is a bug. It has been hidden for 10+ years and seems to be very difficult to fix.
It should be fixed in the long run.

Matz.

Updated by ko1 (Koichi Sasada) 3 months ago

  • Assignee set to ko1 (Koichi Sasada)

Also available in: Atom PDF