Sha256: 165c0fb70ab958cdf69b1cd9711f2022410ef175deb2991d50368c423f9c32bc

Contents?: true

Size: 767 Bytes

Versions: 5

Compression:

Stored size: 767 Bytes

Contents

# frozen_string_literal: true

unless Array.method_defined? :only!
  class Array
    # refactors the same array with only given values (if exists)
    #
    # @example
    #   ary = [:foo, :bar, :bat]
    #   ary.only!(:bar, :moon)
    #   > ary == [:bar]
    #
    # @param [Array] values
    # @return [Array] self
    def only!(*values)
      reject! { |value| !values.include?(value) }
      self
    end
  end
end

unless Array.method_defined? :only
  class Array
    # returns a new array with only given values (if exists)
    #
    # @example
    #   ary = [:foo, :bar, :bat]
    #   ary.only(:bar, :bat, :moon)
    #   > [:bar, :bat]
    #
    # @param [Array] values
    # @return [Array] ary
    def only(*values)
      dup.only!(*values)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby_smart-support-1.5.0 lib/ruby_smart/support/core_ext/ruby/array.rb
ruby_smart-support-1.4.0 lib/ruby_smart/support/core_ext/ruby/array.rb
ruby_smart-support-1.3.0 lib/ruby_smart/support/core_ext/ruby/array.rb
ruby_smart-support-1.2.0 lib/ruby_smart/support/core_ext/ruby/array.rb
ruby_smart-support-1.1.1 lib/ruby_smart/support/core_ext/ruby/array.rb