Sha256: 5dae9822fd26cd1f026b8cb47a9bb78ff37cd2cbb6c459674a1ec865e662e4a7

Contents?: true

Size: 914 Bytes

Versions: 2

Compression:

Stored size: 914 Bytes

Contents

class Range  
  def intersection(other)  
    raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)  
  
    min, max = first, exclude_end? ? max : last  
    other_min, other_max = other.first, other.exclude_end? ? other.max : other.last  
  
    new_min = self === other_min ? other_min : other === min ? min : nil  
    new_max = self === other_max ? other_max : other === max ? max : nil  
  
    new_min && new_max ? new_min..new_max : nil  
  end

  def intersect(other)  
    raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)  

    new_min = self.cover?(other.min) ? other.min : other.cover?(min) ? min : nil  
    new_max = self.cover?(other.max) ? other.max : other.cover?(max) ? max : nil  

    new_min && new_max ? new_min..new_max : nil  

  rescue # if above doesn't work for ruby version
    intersection(other)
  end  
  alias_method :&, :intersect
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sugar-high-0.7.3 lib/sugar-high/range.rb
sugar-high-0.7.2 lib/sugar-high/range.rb