Ruby: intersection between two ranges
require 'date'
class Range
def intersection(other)
return nil if (self.max < other.begin or other.max < self.begin)
[self.begin, other.begin].max..[self.max, other.max].min
end
alias_method :&, :intersection
end
p (Date.new(2011,1,1)..Date.new(2011,1,15)) & (Date.new(2011,1,10)..Date.new(2011,2,15))
#<Date: 2011-01-10 ((2455572j,0s,0n),+0s,2299161j)>..#<Date: 2011-01-15 ((2455577j,0s,0n),+0s,2299161j)>
How can I check if 2 ranges intersect in any way in ruby?
You can check if one range includes begin or end of another one:
r1 = (1..5)
r2 = (4..8)
r1.include?(r2.begin) || r1.include?(r2.end) || r2.include?(r1.begin) || r2.include?(r1.end)
In your case:
r1 = (sprite1[1]..sprite1[1] + sprite1[0].width)
r2 = (sprite2[1]..sprite2[1] + sprite2[0].width)
r1.include?(sprite2[1]) || r1.include?(sprite2[1] + sprite2[0].width) ||
r2.include(sprite1[1]) || r2.include(sprite1[1] + sprite1[0].width)
assuming that the ranges are not endless.
Check if two ranges overlap in ruby
Two ranges overlap for a given range A when:
- range B starts within range A,
- range B ends within range A or
- range B starts before range A and ends after range A
Examples:
Range A |-----|
|-----| Case 1
|-----| Case 2
|-| Case 1 + 2
|---------| Case 3
Looking closer the rule is: Two ranges overlap when Range B starts before the range A ends and range B ends after the range A starts.
def ranges_overlap?(range_a, range_b)
range_b.begin <= range_a.end && range_a.begin <= range_b.end
end
How to combine overlapping time ranges (time ranges union)
Given a function that returns truthy if two ranges overlap:
def ranges_overlap?(a, b)
a.include?(b.begin) || b.include?(a.begin)
end
(this function courtesy of sepp2k and steenslag)
and a function that merges two overlapping ranges:
def merge_ranges(a, b)
[a.begin, b.begin].min..[a.end, b.end].max
end
then this function, given an array of ranges, returns a new array with any overlapping ranges merged:
def merge_overlapping_ranges(overlapping_ranges)
overlapping_ranges.sort_by(&:begin).inject([]) do |ranges, range|
if !ranges.empty? && ranges_overlap?(ranges.last, range)
ranges[0...-1] + [merge_ranges(ranges.last, range)]
else
ranges + [range]
end
end
end
How can I calculate the intersection of two ordinary intervals in Ruby?
This should do it.
Code
def length(a,b,p)
[[a.last,b.last].min - [a.first,b.first].max,0].max.round(p)
end
Examples
Dynamic intersection in Ruby Arrays
arr.reduce &:&
#⇒ [
# [0] 2,
# [1] 4
# ]
Return object after performing intersection of two arrays based on attribute
you can:
1 :
override the eql?(other)
method then the array intersection will work
class Link < ApplicationRecord
def eql?(other)
self.class == other.class && self.id == other&.id # classes comparing class is a guard here
end
# you should always update the hash if you are overriding the eql?() https://stackoverflow.com/a/54961965/5872935
def hash
self.id.hash
end
end
2:
use array.select
:
array_links.flat_map {|i| selected_links.select {|k| k.user_id == i.user_id }}
Related Topics
How to Push to Faye Server from Rails Controller
Building Ruby with Rbenv and Ruby-Build Fails with Undefined Symbol: Sslv2_Method
Combining Multiple Named Scopes with Or
What's the Difference Between Rspec's Subject and Let? When Should They Be Used or Not
Directly Accessing an Instance Variable VS. Using an Accessor Method
Redirect User After Log in Only If It's on Root_Path
Add Space After Commas Only If It Doesn't Already
Reflection on Method Parameters in Ruby
Ruby, !! Operator (A/K/A the Double-Bang)
What Does "||=" Do in Ruby 1.9.2
What Are <-- Ruby Strings Called? and How to Insert Variables in Them
How to Dump an Object's Fields to the Console