README.md in inactive_support-1.1.0 vs README.md in inactive_support-1.2.0

- old
+ new

@@ -1,8 +1,8 @@ # InactiveSupport -A collection of utilities for ruby projects. +A collection of useful extensions for the Ruby programming language. ## Installation Add this line to your application's Gemfile: @@ -14,44 +14,132 @@ Or install it yourself as: $ gem install inactive_support -## Usage +## Extensions -### Object -#### #identity +#### Object + +##### #blank? +Returns true if the object is blank: + +```ruby +[].blank? +# => true + +{}.blank? +# => true + +''.blank? +# => true + +' '.blank? +# => true + +"\n\t".blank? +# => true + +'A man'.blank? +# => false +"" +``` + +##### #identity returns self - [1,2,3,3,4,5,5].group_by(&:identity) - # => [[1], [2], [3,3], [4], [5,5]] +```ruby +[1, 2, 3, 3, 4, 5, 5].group_by(&:identity) +# => [[1], [2], [3, 3], [4], [5, 5]] +``` -#### #try +##### #try send a message to the receiver and if it doesn't respond to the message, return nil - "".try(:some_method) - # => nil +```ruby +'A string'.some_method +# => NoMethodError: undefined method `some_method' for "A string":String -#### #ctry +'A string'.try(:some_method) +# => nil +``` + +##### #ctry chained try, for methods with no arguments - "Somestring".ctry(:mb_chars, :downcase, :some_method) - # => nil +```ruby +"Somestring".ctry(:mb_chars, :downcase, :some_method) +# => nil +``` -### Hash -#### #delete_blank +##### #deep_dup +Object#dup has a hidden pitfall: + +```ruby +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: + +```ruby +person = { name: { first: 'Gavin' } } +dupl = person.deep_dup + +#this also changes person when it shouldn't +dupl[:name][:first] = 'Dazen' + +person +# => {:name=>{:first=>"Gavin"}} +``` + +#### Hash +##### #delete_blank 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 } +```ruby +{ name: nil, age: 19, address: "" }.delete_blank +# => { age: 19 } +``` -### Enumerable -#### #consecutive_by -groups objects by an attribute that is consecutive +##### #deep_delete_blank +Recursively deletes all key/value pairs where the value is an empty string/array/hash or nil. - [1,2,3,5,6,8,9].consecutive_by(&:identity) - # => [[1,2,3],[5,6],[8,9]] +```ruby +{ name: nil, age: 19, address: { street_name: 'Vitosha', street_number: nil }, }.deep_delete_blank +# => { age: 19, address: { street_name: 'Vitosha' } } +``` +#### Enumerable +##### #consecutive_by +Groups objects by an attribute that is consecutive + +```ruby +[1, 2, 3, 5, 6, 8, 9].consecutive_by(&:identity) +# => [[1, 2, 3], [5, 6], [8, 9]] +``` + +##### #consecutive? +Returns true if the objects are consecutive + +```ruby +[1, 2, 3, 5, 6, 8, 9].consecutive? +# => false +``` + +##### #sorted? +Returns true if the collection is sorted + +```ruby +[1, 2, 3, 3, 5, 6, 8, 9].sorted? +# => true +``` ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`)