Sha256: 45bdf1b1a59680bb3d6cabf5a7de81a89eb0da84556c5491791c499ae9c49719

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

module MoreCoreExtensions
  module ArrayInclusions
    #
    # Returns whether the Array contains any of the items.
    #
    #   [1, 2, 3].include_any?(1, 2)  #=> true
    #   [1, 2, 3].include_any?(1, 4)  #=> true
    #   [1, 2, 3].include_any?(4, 5)  #=> false
    def include_any?(*items)
      !(self & items).empty?
    end

    #
    # Returns whether the Array contains none of the items.
    #
    #   [1, 2, 3].include_none?(1, 2)  #=> false
    #   [1, 2, 3].include_none?(1, 4)  #=> false
    #   [1, 2, 3].include_none?(4, 5)  #=> true
    def include_none?(*items)
      (self & items).empty?
    end

    #
    # Returns whether the Array contains all of the items.
    #
    #   [1, 2, 3].include_all?(1, 2)  #=> true
    #   [1, 2, 3].include_all?(1, 4)  #=> false
    #   [1, 2, 3].include_all?(4, 5)  #=> false
    def include_all?(*items)
      (items - self).empty?
    end

    #
    # Returns whether the Array has a value at the index.
    #
    #   [1, 2, 3].includes_index?(-4)  #=> false
    #   [1, 2, 3].includes_index?(-3)  #=> true
    #   [1, 2, 3].includes_index?(1)  #=> true
    #   [1, 2, 3].includes_index?(2)  #=> true
    #   [1, 2, 3].includes_index?(3)  #=> false
    def includes_index?(index)
      (-self.length...self.length).cover?(index)
    end
  end
end

Array.send(:include, MoreCoreExtensions::ArrayInclusions)

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
more_core_extensions-1.2.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-1.1.2 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-1.1.1 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-1.1.0 lib/more_core_extensions/core_ext/array/inclusions.rb