Home > Back-end >  Remove 0 from array ruby
Remove 0 from array ruby

Time:04-08

So I have this array [08,09,10,11] and I want to take off the 0 like this [8,9,10,11] there's any method to do it or any short way?

Thanks!

CodePudding user response:

I believe this is an array strings - ["08", "09", "10", "11"], otherwise it doesn't make sense at all that they are not already valid integers (as you wish without the zero).

So in case you have strings, you can simply do

["08", "09", "10", "11"].map(&:to_i)

CodePudding user response:

Borrowing from flaco's answer - its easier than filtering on a variable length string of zeros

["08", "00","09", "10", "11"].map(&:to_i).delete_if {|item| item == 0}

  • Related