A collection of useful extensions for the Ruby programming language.
Add this line to your application's Gemfile:
gem 'inactive_support'
And then execute:
$ bundle
Or install it yourself as:
$ gem install inactive_support
Returns true if the object is blank:
[].blank?
# => true
{}.blank?
# => true
''.blank?
# => true
' '.blank?
# => true
"\n\t".blank?
# => true
'A man'.blank?
# => false
""
returns self
[1, 2, 3, 3, 4, 5, 5].group_by(&:identity)
# => [[1], [2], [3, 3], [4], [5, 5]]
send a message to the receiver and if it doesn't respond to the message, return nil
'A string'.some_method
# => NoMethodError: undefined method `some_method' for "A string":String
'A string'.try(:some_method)
# => nil
chained try, for methods with no arguments
"Somestring".ctry(:mb_chars, :downcase, :some_method)
# => nil
Object#dup has a hidden pitfall:
person = { name: { first: 'Gavin' } }
dupl = person.dup
#this also changes person when it shouldn't
dupl[:name][:first] = 'Dazen'
person
# => {:name=>{:first=>"Dazen"}}
Object#deep_dup fixes this behavior:
person = { name: { first: 'Gavin' } }
dupl = person.deep_dup
#this also changes person when it shouldn't
dupl[:name][:first] = 'Dazen'
person
# => {:name=>{:first=>"Gavin"}}
Deletes all key/value pairs where the value is an empty string/array/hash or nil.
{ name: nil, age: 19, address: "" }.delete_blank
# => { age: 19 }
Recursively deletes all key/value pairs where the value is an empty string/array/hash or nil.
{ name: nil, age: 19, address: { street_name: 'Vitosha', street_number: nil }, }.deep_delete_blank
# => { age: 19, address: { street_name: 'Vitosha' } }
Groups objects by an attribute that is consecutive
[1, 2, 3, 5, 6, 8, 9].consecutive_by(&:identity)
# => [[1, 2, 3], [5, 6], [8, 9]]
Returns true if the objects are consecutive
[1, 2, 3, 5, 6, 8, 9].consecutive?
# => false
Returns true if the collection is sorted
[1, 2, 3, 3, 5, 6, 8, 9].sorted?
# => true
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request