Skip to content

Commit 9d40678

Browse files
committed
Fix Enumerable#{grep,grep_v} to set $~ correctly and pass specs
1 parent 89e9edc commit 9d40678

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/main/ruby/truffleruby/core/enumerable.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,9 @@ def grep(pattern, &block)
357357
if block_given?
358358
each do
359359
o = Truffle.single_block_arg
360-
if pattern === o
361-
Truffle::RegexpOperations.set_last_match($~, block.binding)
360+
matches = pattern === o
361+
Truffle::RegexpOperations.set_last_match($~, block.binding)
362+
if matches
362363
ary << yield(o)
363364
end
364365
end
@@ -376,25 +377,27 @@ def grep(pattern, &block)
376377
ary
377378
end
378379

379-
def grep_v(pattern)
380+
def grep_v(pattern, &block)
380381
ary = []
381382

382383
if block_given?
383384
each do
384385
o = Truffle.single_block_arg
385-
unless pattern === o
386-
# Regexp.set_block_last_match # TODO BJF Aug 1, 2016 Investigate for removal
386+
matches = pattern === o
387+
Truffle::RegexpOperations.set_last_match($~, block.binding)
388+
unless matches
387389
ary << yield(o)
388390
end
389391
end
390392
else
391393
each do
392394
o = Truffle.single_block_arg
393395
unless pattern === o
394-
# Regexp.set_block_last_match # TODO BJF Aug 1, 2016 Investigate for removal
395396
ary << o
396397
end
397398
end
399+
400+
Truffle::RegexpOperations.set_last_match($~, Truffle.invoke_primitive(:caller_binding))
398401
end
399402

400403
ary

0 commit comments

Comments
 (0)