arrays - Remove leading zeros Ruby -


i want remove leading zeros number.i want use integer(i) instead of i.to_i in order rescue nil.

"011,12,h,013,14".split(",").map{|i| integer rescue nil} 

i want get:

[11,12,nil,13,14] 

instead this:

[9, 12, nil, 11, 14] 

what problem?

from documentation of kernel#integer:

integer(arg, base=0) → integer

[…] if arg string, when base omitted or equals zero, radix indicators (0, 0b, , 0x) honored. […]

you're omitting base leading 0 in strings means base-8 (or octal) used conversion integers. integer literals work same way (011 == 9 yields true). if prefixes/radix indicators shouldn't honored , integers represented strings should treated base-10 (or decimal) one, pass 10 base:

"011,12,h,013,14".split(",").map{|i| integer(i, 10) rescue nil} # => [11, 12, nil, 13, 14] 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -