Ruby - Arrays
Given a hash h = {a: 1, b: nil, c: 3, d: nil}
Which Ruby code removes all key-value pairs where the value is nil using compact?
Given a hash h = {a: 1, b: nil, c: 3, d: nil}
Which Ruby code removes all key-value pairs where the value is nil using compact?
compact and compact! on Hash to remove pairs with nil values.delete_if { |k, v| v.nil? } also removes pairs with nil values by filtering.compact! and delete_if modify the hash in place to remove nil values, so both are correct.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions