Skip to content

Instantly share code, notes, and snippets.

@unicornrainbow
Created September 21, 2011 06:31
Show Gist options
  • Select an option

  • Save unicornrainbow/1231406 to your computer and use it in GitHub Desktop.

Select an option

Save unicornrainbow/1231406 to your computer and use it in GitHub Desktop.
An Alternative (and preferred) wording for the has methods
# all_in, any_in, and none_in extension to Array.
#
class Array
# Returns true if the argument list contain every item of the array.
#
# Basic Usage:
#
# [:black, :blue].all_in :black, :blue # => true
# [:black].all_in :black, :red # => true
# [:black, :blue].all_in :black, :red, :blue # => false
#
def all_in(*ary)
reduce(true) { |memo, obj| memo && ary.include?(obj) }
end
# Returns true if the argument list contains one or more items from the array .
#
# Basic Usage:
#
# [:black, :blue].any_in :black # => true
# [:black, :blue].any_in :black, :red # => true
# [:black, :blue].any_in :green, :red # => false
#
def any_in(*ary)
reduce { |memo, obj| memo || ary.include?(obj) }
end
# Return true is the the argument list has no items in common with the array. (Opsitie of any_in)
#
# [:black, :blue].none_in :black # => false
# [:black, :blue].none_in :black, :red # => false
# [:black, :blue].none_in :green, :red # => true
#
def none_in(*ary)
!any_in(*ary)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment