Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Being impressed with the awesomeness of jstip repository, I came up with the ide
Please feel free to send us a pull request with your Rails tip to be published here. Any improvements or suggestions are more than welcome!

# Tips list
- 30 - [Remove nil from hash in rails 5](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-11-26-Remove_nil_from_hash.md)
- 29 - [Retrieving the ids of the has_many or has_many_through relationships](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-29-Retrieving_ids_from_relationship.md)
- 28 - [Support for left outer join in rails 5](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-10-left_outer_join_in_Rails_5.md)
- 27 - [Render partial from cache faster](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-09-rendering_partial_from_cache_faster.md)
- 26 - [Increase productivity with console tricks](https://github.com/logeshmallow/rails_tips/blob/master/rails_tip/2016-03-08-Increase_productivity_with_few_tricks.md)
Expand Down
25 changes: 25 additions & 0 deletions rails_tip/2016-11-26-Remove_nil_from_hash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Remove nil from hash using Hash#compact and Hash#compact!
tip-number: 30
tip-username: Logesh
tip-username-profile: https://github.com/logeshmallow
tip-description: We always had an issue when we send hash with nil value and now here is the option in ruby 2.4 to remove the nil using Hash#compact and Hash#compact!

---




We all would have faced removing nil value from array and hash and we had compact for removing nil from array and now in ruby 2.4, we also have option to remove it from hash

```ruby

hash = { "username" => "logesh", "company" => nil}
hash.compact #=> { "username" => "logesh" }
hash #=> { "username" => "logesh", "company" => nil}

hash.compact! #=> { "username" => "logesh" }
hash #=> { "username" => "logesh" }
```

Since this comes with ruby itself, we even don't need rails to use this.