Skip to content

Commit

Permalink
Add Range.reverse_each
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Jan 3, 2024
1 parent d2217b1 commit 085dcf9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ itself, JRuby and Rubinius.

- `named_captures` (with `symbolize_keys` option)

#### Range

- `reverse_each` (with correct handling of endless Ranges)

## Ruby 3.2 backports

#### Class
Expand Down
20 changes: 20 additions & 0 deletions lib/backports/3.3.0/range/reverse_each.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
unless (Range.new(nil, 3) rescue [:ok]).reverse_each.first
require 'backports/tools/alias_method_chain'

class Range
def reverse_each_with_endless_handling(&block)
case self.end
when nil
raise "Hey"
when Float::INFINITY
raise "Hey"
when Integer
delta = exclusive? ? 1 : 0
((self.end - delta)..(self.begin)).each(&block)
else
reverse_each_without_endless_handling(&block)
end
end
Backports.alias_method_chain self, :reverse_each, :endless_handling
end
end

0 comments on commit 085dcf9

Please sign in to comment.