新手问题 类似 C 中 scanf 的方法 或者 a="1 2 3 4"转 a=[1,2,3,4] 简单的方法?

pro21ms4 · January 04, 2014 · Last by pro21ms4 replied at January 04, 2014 · 1816 hits

只知道有个 gets 得好几步

"1 2 3 4".split

require 'scanf'
'1 2 3 4'.scanf '%d %d %d %d'

["1", "2", "3", "4"] 再用迭代器所有元素 to_i 太麻烦了吧

#3 楼 @pro21ms4 '1 2 3 4'.split.map &:to_i 也可以

那就 "1 2 3 4".split.map(&:to_i)

2 楼的方法正好适用。

You need to Sign in before reply, if you don't have an account, please Sign up first.