Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kbaird/hash-selectors

A small set of select methods for Ruby Hashes
https://github.com/kbaird/hash-selectors

Last synced: about 1 month ago
JSON representation

A small set of select methods for Ruby Hashes

Awesome Lists containing this project

README

        

hash-selectors
==============

A small set of select methods for Ruby Hashes

# select_by... (or filter_by...)
{a: 1, b: 2, c: 3}.filter_by_keys :a, :b # returns {a: 1, b: 2}
{a: 1, b: 2, c: 3}.select_by_keys :a, :b # returns {a: 1, b: 2}

{a: 1, b: 2, c: 3}.filter_by_values 1, 3 # returns {a: 1, c: 3}
{a: 1, b: 2, c: 3}.select_by_values 1, 3 # returns {a: 1, c: 3}

# reject_by...
{a: 1, b: 2, c: 3}.reject_by_keys :c # returns {a: 1, b: 2}

{a: 1, b: 2, c: 3}.reject_by_values 2 # returns {a: 1, c: 3}

# partition_by...
{a: 1, b: 2, c: 3}.partition_by_keys :a, :b # returns [{a: 1, b: 2}, {c: 3}]

{a: 1, b: 2, c: 3}.partition_by_values 1, 3 # returns [{a: 1, c: 3}, {b: 2}]

# values_for_keys
{a: 1, b: 2, c: nil}.values_for_keys :a, :c # returns [1, nil]

# unfiltered_values_for_keys
{a: 1, b: 2, c: nil}.unfiltered_values_for_keys :a, :c, :d # returns [1, nil, nil]