Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove numbers from array of strings

I have an array that looks like this:

["lorem", "ipsum", "1734", "dolor", "1", "301", "et", "4102", "92"]

Is there a way to remove all the numbers in the array, even though they're stored as strings, so that I'd be left with this:

["lorem", "ipsum", "dolor", "et"]

Thanks for any hints.

like image 913
simonrohrbach Avatar asked Dec 15 '22 18:12

simonrohrbach


1 Answers

Use a regexp pattern

s = ["lorem", "ipsum", "1734", "dolor", "1", "301", "et", "4102", "92"]
s.reject { |l| l =~ /\A\d+\z/ }
# => ["lorem", "ipsum", "dolor", "et"] 
like image 129
Simone Carletti Avatar answered Dec 31 '22 01:12

Simone Carletti