filter

The filter method uses an attached block to decide it's behaviour. This block can take either 1 or 2 parameters, and passes in different values depending on this.

.filter() { |value| ... }

When calling filter you attach a do block to state which elements you wish to keep. A new hash is then returned that contains just those elements.

If the block returns null, false or fails to return anything then the key-value given is not kept. Returning anything else will keep the value.

myHash  = { :a : 1, :b : 30, :c : 2, :d : 9 }
newHash = myHash.filter() do |value|
    return value > 5
end

.filter() { |key, value| ... }

If the block contains 2 parameters, then the each key and value is passed into the block (in that order).

myHash  = { :a : 1, :b : 30, :c : 2, :d : 9 }
newHash = myHash.filter() do |key, value|
    return ( key == :a ) || ( value > 5 )
end