Ruby 中的“upto”方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9728075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The 'upto' method in Ruby
提问by Billjk
I'm learning Ruby, and there has been a bit of talk about the uptomethod in the book from which I am learning. I'm confused. What exactly does it do?
我正在学习 Ruby,并且在我学习upto的书中有一些关于方法的讨论。我糊涂了。它究竟有什么作用?
Example:
例子:
grades = [88,99,73,56,87,64]
sum = 0
0.upto(grades.length - 1) do |loop_index|
sum += grades[loop_index]
end
average = sum/grades.length
puts average
回答by knut
Let's try an explanation:
让我们尝试一个解释:
You define an array
你定义一个数组
grades = [88,99,73,56,87,64]
and prepare a variable to store the sum:
并准备一个变量来存储总和:
sum = 0
grades.lengthis 6 (there are 6 elements in the array), (grades.length - 1)is 5.
grades.length是 6(数组中有 6 个元素),(grades.length - 1)是 5。
with 0.upto(5)you loop from 0 to 5, loop_indexwill be 0, then 1...
随着0.upto(5)你从 0 到 5 循环,loop_index将是 0,然后是 1...
The first element of the array is grades[0](the index in the array starts with 0).
That's why you have to subtract 1 from the number of elements.
数组的第一个元素是grades[0](数组中的索引从 0 开始)。这就是为什么你必须从元素数中减去 1。
0.upto(grades.length - 1) do |loop_index|
Add the loop_index's value to sum.
将 loop_index 的值添加到 sum。
sum += grades[loop_index]
end
Now you looped on each element and have the sum of all elements of the array.
现在您循环了每个元素并获得了数组所有元素的总和。
You can calculate the average:
您可以计算平均值:
average = sum/grades.length
Now you write the result to stdout:
现在您将结果写入标准输出:
puts average
This was a non-ruby-likesyntax. Ruby-like you would do it like this:
这是一种非 ruby 式语法。像 Ruby 一样,你会这样做:
grades = [88,99,73,56,87,64]
sum = 0
grades.each do |value|
sum += value
end
average = sum/grades.length
puts average
Addendum based on Marc-Andrés comment:
基于 Marc-Andrés 评论的附录:
You may use also injectto avoid to define the initial sum:
您也可以使用inject来避免定义初始总和:
grades = [88,99,73,56,87,64]
sum = grades.inject do |sum, value|
sum + value
end
average = sum / grades.length
puts average
Or even shorter:
或者更短:
grades = [88,99,73,56,87,64]
average = grades.inject(:+) / grades.length
puts average
回答by Oliver Charlesworth
From http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_integer.html#upto:
从http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_integer.html#upto:
upto
int.upto( anInteger ) {| i | block }Iterates
block, passing in integer values fromintup to and includinganInteger.5.upto(10) { |i| print i, " " }produces:
5 6 7 8 9 10
取决于
int.upto( anInteger ) {| i | block }迭代
block,传递从intup 到 并包括 的整数值anInteger。5.upto(10) { |i| print i, " " }产生:
5 6 7 8 9 10
回答by teleolurian
Upto executes the block given once for each number from the original number "upto" the argument passed. For example:
Upto 为从原始数字“直到”传递的参数的每个数字执行一次给定的块。例如:
1.upto(10) {|x| puts x}
will print out the numbers 1 through 10.
将打印出数字 1 到 10。
回答by ScottJShea
It is just another way to do a loop/iteratorin Ruby. It says do this action ntimes based on i being the first number the the number in parens as the limit.
这只是在 Ruby 中执行循环/迭代器的另一种方式。它说根据 i 是第一个数字执行此操作n次,以括号中的数字为限制。
回答by jndaigle
An alternative that looks more like Ruby to me is
对我来说看起来更像 Ruby 的另一种选择是
require 'descriptive_statistics'
grades=[88,99,73,56,87,64]
sum = grades.sum
average = grades.mean
sd = grades.standard_deviation
Of course it depends what you're doing.
当然,这取决于你在做什么。
回答by Chris Pettitt
My example would have been this:
我的例子是这样的:
1.upto(5) { |i| puts "Countup: #{i}" }
So what you're actually doing here is saying, I want to count up from 1 to the number 5, that's specifically what this part is saying:
所以你在这里实际上是在说,我想从 1 数到数字 5,这就是这部分所说的:
1.upto(5)
The latter part of code (a block) is just outputting the iteration of going through the count from 1 up to 5. This is the output you might expect to see:
代码的后半部分(一个块)只是输出从 1 到 5 计数的迭代。这是您可能希望看到的输出:
Countup: 1
Countup: 2
Countup: 3
Countup: 4
Countup: 5
Note: This can be written is another way if you're using multilines:
注意:如果您使用多行,可以用另一种方式编写:
1.upto(5) do |i|
puts "Countup: #{i}"
end
Hope this helps.
希望这可以帮助。

