这个Python片段的最短ruby等价物

时间:2014-11-29 12:25:16

标签: python ruby string

我正在寻找这个Python代码段的最短红宝石

a, b, c, d = map(int, raw_input().split(" ")) 

有什么比这短吗?

a, b, c, d = gets.split(" ").map &:to_i

2 个答案:

答案 0 :(得分:2)

没有参数,String#split将字符串拆分为空格($;):

"1 2 3 4\n".split
# => ["1", "2", "3", "4"]

换句话说,您无需致电chomp删除尾随换行符:

a, b, c, d = gets.split.map &:to_i

Python的

str.split是相似的(不需要指定参数):

>>> "1 2 3 4\n".split()
['1', '2', '3', '4']

答案 1 :(得分:1)

split模式的默认值为$;$;的默认值为nilnil的模式意味着"在空格上分开",它不是100%等同于你拥有的("拆分在单个空格字符"),但是如果你可以忍受,然后你可以缩短你的表达:

a, b, c, d = gets.split.map &:to_i