Sha256: edc1e8d556c949426a41540345804a5eef65eb3defbab8ac3a87cd9edaf6d446

Contents?: true

Size: 1.56 KB

Versions: 18

Compression:

Stored size: 1.56 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)
      items = items.first if items.length == 1 && items.first.kind_of?(Array)
      !(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)
      items = items.first if items.length == 1 && items.first.kind_of?(Array)
      (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 = items.first if items.length == 1 && items.first.kind_of?(Array)
      (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

18 entries across 18 versions & 1 rubygems

Version Path
more_core_extensions-4.5.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-4.4.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-4.3.1 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-4.3.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-4.2.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-4.1.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-4.0.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.8.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.7.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.6.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.5.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.4.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.3.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.2.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.1.1 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.1.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-3.0.0 lib/more_core_extensions/core_ext/array/inclusions.rb
more_core_extensions-2.0.0 lib/more_core_extensions/core_ext/array/inclusions.rb