Answers for "how to remove nil values from an array in ruby"

0

remove nil in array ruby

# A simple example function, which returns a value or nil
def transform(n)
  rand > 0.5 ? n * 10 : nil }
end

items.map! { |x| transform(x) } # [1, 2, 3, 4, 5] => [10, nil, 30, 40, nil]
items.reject! { |x| x.nil? } # [10, nil, 30, 40, nil] => [10, 30, 40]
Posted by: Guest on January-28-2021

Code answers related to "how to remove nil values from an array in ruby"

Browse Popular Code Answers by Language