Aromat

Small extensions to Ruby core libraries to make your life easier

Presentation

This library provides a few extensions to the Ruby core libraries in order to simplify use of some common patterns.

Installation

Gemfile

gem 'aromat'

Terminal

gem install -V aromat

Usage

Symbolize Keys

A sym_keys method is monkey-patched into the Array and Hash classes, and provides a way to recursively convert keys to symbols. This is mostly useful when loading Hashes from external resources (YAML, CSV, JSON, etc...), where keys will be stored as strings.

a = { 'foo' => 'bar', 'nested' => [{ 'problems' => 'none' }] }
a.sym_keys
# => {:foo=>"bar", :nested=>[{:problems=>"none"}]}

Deep-clone

A dclone method is monkey-patched into the Array and Hash classes, and provides a way to recursively deep-clone an instance.

a = { foo: 'bar', bar: :kill_me, nested: [{ problems: 'none' }] }
b = a.dclone
b[:nested] << :foo
b[:nested][:problems] = 'no way'
b[:test] = :bork
b[:foo] = nil
b.delete :bar
a
# => {:foo=>nil, :nested=>[{:problems=>"no way"}, :foo], :test => :bork}
b
# => {:foo=>"bar", :nested=>[{:problems=>"none"}]}

Padding

Two padding methods rpad and lpad are monkey-patched into the String class, and provide a way to pad any string to a given length using a given filler string.

a = 'foo'
a.rpad 10
# => "foo       "
a.rpad 10, '-'
# => "foo-------"
a.rpad 2, '-'
# => "fo"
a.lpad 10
# => "       foo"
a.lpad 10, '-'
# => "-------foo"
a.rpad 2, '-'
# => "oo"

License

The gem is available as open source under the terms of the MIT License.