How can I remove empty hash's from an array?
[{}, {}, :one, :two]
.compact does not seem to work.
How can I remove empty hash's from an array?
Using reject, to reject elements in an enumerable by checking if the element is a Hash and is empty ({}):
p [{}, {}, :one, :two, [], [], '', nil].reject { |e| e == {} }
# [:one, :two, [], [], "", nil]
select, until I saw your answer. I feel reject is much more readable than select with negative conditions in the block.Try Array#reject unwanted elements:
ary = [{}, {}, :one, :two]
ary.reject! { |h| h.empty? }
ary
#=> [:one, :two]
[{}, {}, :one, :two] - [{}]is another way that hasn't been mentioned in the answers.