Ruby - Arrays
You have an array data = [nil, 0, false, '', nil, 5]. You want to remove only nil values but keep 0, false, and empty strings. Which code correctly does this?
You have an array data = [nil, 0, false, '', nil, 5]. You want to remove only nil values but keep 0, false, and empty strings. Which code correctly does this?
compact doescompact removes only nil values and keeps all other values including 0, false, and empty strings.select { |v| v } removes all falsy values (nil, false, 0 is truthy in Ruby but empty string is truthy), so it removes false. reject!(&:nil?) modifies the original array. delete_if { |v| v.nil? } modifies original array.compact is simplest and returns new array without modifying original.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions