Ruby - Arrays
Examine the Ruby code below:
arr = [5, nil, 8, nil, 10] arr.compact puts arr.inspect
Why does the output still include nil values?
Examine the Ruby code below:
arr = [5, nil, 8, nil, 10] arr.compact puts arr.inspect
Why does the output still include nil values?
compact behaviorcompact returns a new array without nil values but does not change the original array.arr.compact is called without assignment, the original arr remains unchanged.compact! modifies in place but is not used here.puts correctly displays arrays.compact returns a new array and does not modify arr in place. -> Option Barr = arr.compact to remove nils from original [OK]15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions