简体   繁体   中英

Remove 0 from array ruby

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!

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)

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM