Beef (beefup)

Beef up your Ruby core with these useful extensions

Installation

Add this line to your application's Gemfile:

  gem 'beefup'

And then execute:

$ bundle

Or install it yourself as:

$ gem install beefup

And require it:

  require 'beefup'

Usage

String

To Boolean

  "true".to_boolean
    => true
  "yes".to_b
    => true
  "no".to_b
    => false
  "false".to_b
    => false
  "something that isn't falsey".to_boolean
    => true

Titleise

Smarter titleising of a string which ignores certain words (E.g an, a, is, of, etc..) when they don't appear at the beginning or the end of the string.

  "is this an original string? yes it is".titleise
    => "Is This an Original String? Yes It Is"

  "this is a title".titleise
    => "This is a Title"

Fixnum

To Boolean

  1.to_boolean
    => true
  0.to_b
    => false
  25.to_b
    => true

Hash

Method Access

Easy access to hash values via method calls of the keys.

  hash = {one: 1, two: 2, three: 3, four: 4}

  # Value access
  hash.one
    => 1

  # Key query
  hash.one?
    => true
  hash.five?
    => false
  hash.five
    => NoMethodError

  # Value assignment
  hash.two = "new value"
  hash.two
    => "new value"

Deep Merge

Recursively merge two hashes

  hash_1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => { :c2a => "c2a" } } }
  hash_2 = { :a => 1, :c => { :c1 => 2, :c2 => { :c2b => "c2b" } } }
  hash_1.deep_merge(hash_2) 
    => { :a => 1, :b => "b", :c => { :c1 => 2, :c2 => { :c2a => "c2a", :c2b => "c2b" } } }

Array Access

Access a nested Hash element with a given array depicting the path

  hash = { :a => {:b => {:c => { :d => "Value" } } } }
  array = [:a, :b, :c, :d]
  hash.access_by_array(array)
    => "Value"

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request