* [ruby-core:115369] [Ruby master Bug#11808] Different behavior between Enumerable#grep and Array#grep
[not found] <redmine.issue-11808.20151211235302.10285@ruby-lang.org>
2020-10-26 4:36 ` [ruby-core:100548] [Ruby master Bug#11808] Different behavior between Enumerable#grep and Array#grep ko1
@ 2023-11-13 19:27 ` noelrap (Noel Rappin) via ruby-core
1 sibling, 0 replies; 2+ messages in thread
From: noelrap (Noel Rappin) via ruby-core @ 2023-11-13 19:27 UTC (permalink / raw)
To: ruby-core; +Cc: noelrap (Noel Rappin)
Issue #11808 has been updated by noelrap (Noel Rappin).
This appears to be fixed in 3.3.0dev as of Nov 2023,
``` ruby
irb(main):001* class Test
irb(main):002* include Enumerable
irb(main):003* def each
irb(main):004* return enum_for(:each) unless block_given?
irb(main):005* yield "Hello"
irb(main):006* yield "World"
irb(main):007* end
irb(main):008> end
=> :each
irb(main):009>
irb(main):010> enum = Test.new
=> #<Test:0x0000000102ba5038>
irb(main):011> array = ["Hello", "World"]
=> ["Hello", "World"]
irb(main):012>
irb(main):013> enum.grep(/^(.)/) {$1}
=> ["H", "W"]
```
However, it was still broken in 3.2.2. It's not clear to me when the behavior changed.
----------------------------------------
Bug #11808: Different behavior between Enumerable#grep and Array#grep
https://bugs.ruby-lang.org/issues/11808#change-105298
* Author: BenOlive (Ben Olive)
* Status: Open
* Priority: Normal
* 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/postorius/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 2+ messages in thread