Ruby - Arrays
What is the output of the following Ruby code?
arr = [nil, 1, nil, 2, 3] new_arr = arr.compact puts new_arr.inspect
What is the output of the following Ruby code?
arr = [nil, 1, nil, 2, 3] new_arr = arr.compact puts new_arr.inspect
compact method removes all nil values from the array, so [nil, 1, nil, 2, 3] becomes [1, 2, 3].puts new_arr.inspect prints the array as a string showing all elements, so the output is [1, 2, 3].15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions