Ruby - Arrays
Find the error in this Ruby code snippet:
arr = [1, nil, 2] new_arr = arr.compact! puts new_arr
Find the error in this Ruby code snippet:
arr = [1, nil, 2] new_arr = arr.compact! puts new_arr
compact! returns nil if no nil values were removed, otherwise returns the modified array.arr contains one nil, compact! removes it and returns the modified array, so new_arr is the array without nil.arr had no nils, compact! would return nil, and puts new_arr would print nothing, which can be confusing.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions