Cameron Bass Cameron Bass - 2 months ago 8
Ruby Question

Delete Strings from an array in Ruby

Lets say I have an Array that looks like this:

arr = [ 1, 5, "dog", 6 ]


How can I delete the String so that my output will look like this:

[ 1, 5, 6 ]

Answer

try this:

arr.select! { |el| el.class != String }

or if you want only numbers:

arr.select! { |el| el.is_a? Numeric }