Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jGRUBBS/b8b7a3cce9fbc3808f8fff5b25c342b1 to your computer and use it in GitHub Desktop.
Save jGRUBBS/b8b7a3cce9fbc3808f8fff5b25c342b1 to your computer and use it in GitHub Desktop.
An RSpec matcher to check for intersection between two enumerables
Spec::Matchers.define :intersect_with do |challenge_range|
# Check to see if any elements exist in the intersection
match do |current_range|
[current_range.to_a & challenge_range.to_a].flatten.any?
end
failure_message_for_should do |current_range|
"#{current_range.inspect} doesn't overlap with #{challenge_range.inspect}"
end
failure_message_for_should_not do |current_range|
"#{current_range.inspect} overlaps with #{challenge_range.inspect}"
end
description do
"intersection of an enumerable's elements"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment