如何显示在Ruby中数组的所有的整数? [英] how to show all integers of an array in ruby?

查看:91
本文介绍了如何显示在Ruby中数组的所有的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的红宝石。试图让所有的号码与方法的数组。

  X = [1..10]

预期的结果。

  => [1,2,3,4,5,6,7,8,9,10]


解决方案

当你键入 [1..10] ,你实际上有什么是的阵列包含一个单独的范围对象。如果你想FixNums数组,你居然降 [] 和调用 to_a 的范围本身:

  IRB(主):006:0> X =(1..10).to_a
= GT; [1,2,3,4,5,6,7,8,9,10]

I'm new for ruby. Trying to get all numbers in an array with an method.

x = [1..10]

expected result.

=> [1,2,3,4,5,6,7,8,9,10]

解决方案

When you type [1..10], what you actually have is an Array containing a single Range object. If you want an array of FixNums, you actually drop the []s and call to_a on the range itself:

irb(main):006:0> x = (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

这篇关于如何显示在Ruby中数组的所有的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆