新手问题 数字转数组

chyanog · July 25, 2012 · Last by quakewang replied at July 26, 2012 · 3473 hits

如:1428------> [1,4,2,8](数组元素需要是整数) 尽量用最简洁的代码实现

1428.to_s.split('').map(&:to_i)

先来一个

1234.to_s.split ""

1428.to_s.chars.map(&:to_i)

1428.to_s.unpack('C*').map{|x| x-48}

1428.to_s.bytes.map{|x| x-48} 这个 map 里面能用&吗

#5 楼 @chyanog

1428.to_s.bytes.each_with_object(48).map(&:-)

·n=1428;(0...n.to_s.length).map{|x| n.to_s[x].to_i}·

eval "[#{1428.to_s.split('').join ','}]"

ruby 1.9

1428.to_s.each_char.map(&:to_i)
You need to Sign in before reply, if you don't have an account, please Sign up first.