Sha256: 4ade71815b7892503a3cbb96160a5cfa788957c144506aa55f5e65ff36ca2577

Contents?: true

Size: 1.02 KB

Versions: 19

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

class Range
  # Compare two ranges and see if they overlap each other
  #  (1..5).overlap?(4..6) # => true
  #  (1..5).overlap?(7..9) # => false
  unless Range.method_defined?(:overlap?)
    def overlap?(other)
      raise TypeError unless other.is_a? Range

      self_begin = self.begin
      other_end = other.end
      other_excl = other.exclude_end?

      return false if _empty_range?(self_begin, other_end, other_excl)

      other_begin = other.begin
      self_end = self.end
      self_excl = self.exclude_end?

      return false if _empty_range?(other_begin, self_end, self_excl)
      return true if self_begin == other_begin

      return false if _empty_range?(self_begin, self_end, self_excl)
      return false if _empty_range?(other_begin, other_end, other_excl)

      true
    end

    private
    def _empty_range?(b, e, excl)
      return false if b.nil? || e.nil?

      comp = b <=> e
      comp.nil? || comp > 0 || (comp == 0 && excl)
    end
  end

  alias :overlaps? :overlap?
end

Version data entries

19 entries across 19 versions & 6 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.5.1 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.5 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.4.2 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.4.1 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.4 lib/active_support/core_ext/range/overlap.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/range/overlap.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/range/overlap.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.3.4 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.3.2 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.3.1 lib/active_support/core_ext/range/overlap.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.3 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.2 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.1 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.0 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.0.rc2 lib/active_support/core_ext/range/overlap.rb
activesupport-7.1.0.rc1 lib/active_support/core_ext/range/overlap.rb