Core_Ext

This project adds some new methods to the core ruby classes

Array

map_to_hash

map returning a hash with the original array for keys

array = %w(a ab)
arrays.map_to_hash { |val| val.length }
{ 'a' => 1, 'b' => 2 }

chain_map

applies map in a chain

array = [ :a, :long_name, :sym ]
array.chain_map(:to_s, :size, :to_s)
[ '1', '9', '3' ]
array = [ :a, :long_name, :sym ]
array.chain_map(:to_s, :size) { |v| "final: #{v}" }
[ 'final: 1', 'final: 9', 'final: 3' ]

as_hash

Creates a hash from the array using the argumen array as keys

  [1, 2, 3].as_hash %w(a b c)

returns

  { 'a' => 1, 'b' => 2, 'c' => 3 } }

Hash

map_to_hash

map returning a hash with the original keys

hash = { a: 1, b: 2 }
hash.map_to_hash { |k, v| "#{k}_#{v}" }
{ a: "a_1", b: "b_2" }

chain_fetch

Applies fetch in a chain

{ a: { b: { c: { d: 10 } } } }.chain_fetch(:a, :b, :c, :d)
10
h = { a: { b: { c: { d: 10 } } } }
h.chain_fetch(:a, :x, :y, :z) { |key, missed_keys| "returned #{key}" }
'returned x'

squash

Squash a deep hash into a simple level hash

  { a: { b:1 } }.squash

returns

  { 'a.b' => 1 }

to_deep_hash

Changes a hash spliting keys into inner hashs

  { 'a.b' => 1 }.to_deep_hash

returns

  { 'a' => { 'b' => 1 } }

camelize_keys

Change the keys camelizing them

  { ca_b: 1 }.camelize_keys

returns

  { CaB: 1 }

change_keys

Change the array keys using a block

  { ca_b: 1 }.change_keys { |k| k.to_s.upcase }

returns

  { 'CA_B' => 1 }

chain_change_keys

Change the hash keys usin a chained method call

  { ca_b: 1 }.chain_change_keys(:to_s, :upcase, :to_sym)

returns

  { CA_B: 1 }

change_values

Change the values of the array

  { a: 1 }.change_keys { |v| (v+1).to_s }

returns

  { a: '2' }

prepend_to_keys

Change each keys prepending an string

  { key: 1 }.prepend_to_keys 'scope:'

returns

  { :'scope:key' => 1 }

append_to_keys

Change each keys appending an string

  { key: 1 }.append_to_keys 's'

returns

  { keys: 1 }

sort_keys

Sort the hash usig the keys

  { b:1, a:2 }.sort_keys

returns

  { a:2, b:1 }

Enumerable

clean!

CLeans empty values from a hash

{ a: 1, b: [], c: nil, d: {}, e: '', f: { b: [], c: nil, d: {}, e: '' } }.clean!

returns

  {}