Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Ruby数组及其打印方法_Arrays_Ruby - Fatal编程技术网

Arrays Ruby数组及其打印方法

Arrays Ruby数组及其打印方法,arrays,ruby,Arrays,Ruby,我在这个网站上看到了一个Ruby的一行程序,但我忘记了: 如果我需要用整数打印某些操作的结果: print {|e| e = e * e} 但是,我如何让Ruby知道e是,比如说,1到10?不太清楚您想要做什么,但这里有一个输入 (1..10).to_a 这将为您提供一个从1到10的数字数组。在您的范围内使用括号 (1..10).map {|ele| ele + 3} // 1 to 10 including the last number (1...10).map {|ele| ele +

我在这个网站上看到了一个Ruby的一行程序,但我忘记了:

如果我需要用整数打印某些操作的结果:

print {|e| e = e * e}

但是,我如何让Ruby知道e是,比如说,1到10?

不太清楚您想要做什么,但这里有一个输入

(1..10).to_a

这将为您提供一个从1到10的数字数组。

在您的范围内使用括号

(1..10).map {|ele| ele + 3} // 1 to 10 including the last number
(1...10).map {|ele| ele + 3 } // 1 to 9 excluding the last number
“10.times do…”或“1..10.each do”或…
1.最多(10){e|e | puts e*e}
会起作用。“map”起作用,但明显不同于“each”。