Skip to content

Commit d7cc826

Browse files
committed
Remove duplication in Enumerator::Lazy#grep
1 parent 392e22d commit d7cc826

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -391,26 +391,17 @@ def reject
391391
end
392392

393393
def grep(pattern, &block)
394-
if block_given?
395-
Lazy.new(self, nil) do |yielder, *args|
396-
val = args.length >= 2 ? args : args.first
397-
matches = pattern === val
398-
Truffle::RegexpOperations.set_last_match($~, block.binding)
399-
400-
if matches
401-
yielder.yield yield(val)
402-
end
403-
end
404-
else
405-
# :caller_binding is not optimized in blocks right now, and we want the caller of #grep
406-
caller_binding = Truffle.invoke_primitive(:caller_binding)
394+
binding = block ? block.binding : Truffle.invoke_primitive(:caller_binding)
407395

408-
Lazy.new(self, nil) do |yielder, *args|
409-
val = args.length >= 2 ? args : args.first
410-
matches = pattern === val
411-
Truffle::RegexpOperations.set_last_match($~, caller_binding)
396+
Lazy.new(self, nil) do |yielder, *args|
397+
val = args.length >= 2 ? args : args.first
398+
matches = pattern === val
399+
Truffle::RegexpOperations.set_last_match($~, binding)
412400

413-
if matches
401+
if matches
402+
if block
403+
yielder.yield yield(val)
404+
else
414405
yielder.yield val
415406
end
416407
end

0 commit comments

Comments
 (0)