[ruby-core:93422] [Ruby trunk Feature#11808] DIfferent behavior between Enumerable#grep and Array#grep

From: nobu@...
Date: 2019-06-30 00:13:36 UTC
List: ruby-core #93422
Issue #11808 has been updated by nobu (Nobuyoshi Nakada).

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

There is no API for it now.

https://github.com/nobu/ruby/tree/feature/11808-block_svar

----------------------------------------
Feature #11808: DIfferent behavior between Enumerable#grep and Array#grep
https://bugs.ruby-lang.org/issues/11808#change-78963

* Author: BenOlive (Ben Olive)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next