Skip to content

Instantly share code, notes, and snippets.

@jherdman
Created December 2, 2009 22:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jherdman/247669 to your computer and use it in GitHub Desktop.
Save jherdman/247669 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