_.set, _.get, and _.unset for Ruby
The two methods set and get are based on Lodash and ported to Ruby, along with their unit tests.
gem install rodash
object = { 'a' => [{ 'b' => { 'c' => 3 } }] }
Rodash.set(object, 'a[0].b.c', 4)
object['a'][0]['b']['c']
=> 4
Rodash.set(object, 'x[0].y.z', 5)
object['x'][0]['y']['z'])
=> 5
object = { 'a' => [{ 'b' => { 'c' => 3 } }] }
Rodash.get(object, 'a[0].b.c')
=> 3
Rodash.get(object, ['a', '0', 'b', 'c'])
=> 3
Rodash.get(object, 'a.b.c', 'default')
=> 'default'
object = { 'a' => [{ 'b' => { 'c' => 7 } }] }
Rodash.unset(object, 'a[0].b.c')
=> true
object
=> { 'a' => [{ 'b' => {} }] }
Rodash.unset(object, 'a[0].b.c')
=> true
object
=> { 'a' => [{ 'b' => {} }] }