Module: Bendy

Defined in:
lib/bendy.rb,
lib/bendy/hash.rb,
lib/bendy/array.rb,
lib/bendy/object.rb,
lib/bendy/logical.rb,
lib/bendy/version.rb

Overview

Bendy module that _freedom patches_ Object and NilClass to add ‘try`, like in ActiveSupport

Defined Under Namespace

Modules: Logical

Constant Summary collapse

VERSION =
"0.3.3"

Instance Method Summary collapse

Instance Method Details

#get_deepObject

get_deep digs into a hash along the “route” given by fields elements:

{ foo: { bar: { baz: 1 } } }.get_deep(:foo, :bar, :baz)
# => 1

{ foo: { bar: { baz: 1 } } }.get_deep(:foo, :bar, :baz, :quux)
# => nil


16
17
18
19
20
# File 'lib/bendy/hash.rb', line 16

refine Hash do
  def get_deep(*fields)
    fields.inject(self) {|acc,e| acc[e] if acc.is_a?(Hash)}
  end
end

#initsObject

inits produces all sub-arrays by successively taking leading elements:

[1, 2, 3].inits
# => [[], [1], [1, 2], [1, 2, 3]]


26
27
28
29
30
# File 'lib/bendy/array.rb', line 26

refine Array do
  def inits
    (self.length + 1).times.map{ |i| self.take(i) }
  end
end

#tailsObject

tails produces all sub-arrays by successively dropping leading elements:

[1, 2, 3].tails
# => [[1, 2, 3], [2, 3], [3], []]


13
14
15
16
17
# File 'lib/bendy/array.rb', line 13

refine Array do
  def tails
    (self.length + 1).times.map{ |i| self.drop(i) }
  end
end