0

My array of hash is like:

[
  {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"},
  {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, 
  {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, 
  {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}, 
  {:id=>nil, :name=>nil, :type=>nil}, 
  {:id=>nil, :name=>nil, :type=>nil},
  {:id=>nil, :name=>nil, :type=>nil},
  {:id=>nil, :name=>nil, :type=>nil}, 
  {:id=>nil, :name=>nil, :type=>nil}, 
  {:id=>nil, :name=>nil, :type=>nil}, 
  {:id=>nil, :name=>nil, :type=>nil}
]

How can I remove nil values if in my hash id is nil?

1
  • 3
    Please edit to clarify (even though you've selected an answer, as many people may read your question in future). "How can I remove nil value if my hash id is nil? doesn't make sense. You can remove values without removing keys. Perhaps you mean you want to remove all hashes h for which h[:id].nil? #=> true? If so, say so (and if your array is a, do you want to modify a or return an array equal to a with some (hash) elements removed?), Be precise in all things Ruby! Commented Jul 20, 2015 at 6:53

4 Answers 4

5

You can use reject:

arr.reject{|h| h[:id].nil?}

=> [{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"},
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"},
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"},
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}]

Demo

Or select:

arr.select { |h| h[:id] }

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

If your concern is to remove element which hash id is nil then better to use delete_if

array.delete_if { |h| h[:id].nil? }
 => [
      {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, 
      {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"},
      {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, 
      {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}
    ]

Note: reject will returns a new array containing the items in self for which the given block is not true. While delete_if will Deletes every element of self for which block evaluates to true.

Comments

0

I assume you want to get all hashes whose values are not all nil. You can do that by:

arr = [{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}]

arr.find_all { |h| !h.values.compact.empty? }
# => [{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}]

Comments

0

You want to use the select method to filter the array.

list = list.select { |item| item[:id] != nil }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.