From: "wanabe (_ wanabe) via ruby-core" Date: 2026-08-16T08:11:55+00:00 Subject: [ruby-core:126401] [Ruby Bug#11808] Different behavior between Enumerable#grep and Array#grep Issue #11808 has been updated by wanabe (_ wanabe). Status changed from Assigned to Closed It has been fixed at 0a82bfe5e18ac86da72c27389db6eb8da156a0b5 == https://github.com/ruby/ruby/pull/7225 ``` $ cat a.rb 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"] p enum.grep(/^(.)/) {$1} p array.grep(/^(.)/) {$1} $ git checkout 0a82bfe5e18ac86da72c27389db6eb8da156a0b5~ && make -j miniruby > /dev/null 2>&1 && ./miniruby a.rb HEAD is now at 2675f2c864f Remove whitespace [nil, nil] ["H", "W"] $ git checkout 0a82bfe5e18ac86da72c27389db6eb8da156a0b5 && make -j miniruby > /dev/null 2>&1 && ./miniruby a.rb Previous HEAD position was 2675f2c864f Remove whitespace HEAD is now at 0a82bfe5e18 use correct svar (#7225) ["H", "W"] ["H", "W"] ``` This commit is included in 3.3. Since 3.2 is end-of-life, there is no need to backport it. ---------------------------------------- Bug #11808: Different behavior between Enumerable#grep and Array#grep https://bugs.ruby-lang.org/issues/11808#change-118539 * Author: BenOlive (Ben Olive) * Status: Closed * Assignee: ko1 (Koichi Sasada) * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- 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: ~~~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 -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/