Sha256: b0d83a165225226f05f1f1b4292ed357daff0bbdf0959a09ad567b910f2c5b23

Contents?: true

Size: 552 Bytes

Versions: 2

Compression:

Stored size: 552 Bytes

Contents

# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

class Hash
  # Returns the array of keys of an occurrence of given +values+.
  # If the value is not found, returns empty array.
  #
  # ==== Arguments
  #
  # * +values+ - +values+ for which keys are to be retrieved.
  #
  # ==== Examples
  #
  #   {a: 1, b: 2}.keys_at(1, 2) #=> [:a, :b]
  #   {a: 1, b: 2}.keys_at(1, 3) #=> [:a]
  #   {a: 1, b: 2}.keys_at(3)    #=> []
  def keys_at(*values)
    select { |_, value| values.include?(value) }.keys
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rb_core_extensions-0.1.3 lib/core_extensions/hash/access.rb
rb_core_extensions-0.1.2 lib/core_extensions/hash/access.rb