The upto method in Ruby

I am learning Ruby, and in the book I am learning from, there was little talk about the upto method. I'm confused. What exactly is he doing?

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 
+6
source share
6 answers

Try the explanation:

You define an array

 grades = [88,99,73,56,87,64] 

and prepare a variable to store the amount:

 sum = 0 

grades.length is 6 (there are 6 elements in the array), (grades.length - 1) is 5.

with 0.upto(5) you loop from 0 to 5, loop_index will be 0, then 1 ...

The first element of the array is grades[0] (the index in the array starts at 0). That is why you must subtract 1 from the number of elements.

 0.upto(grades.length - 1) do |loop_index| 

Add the loop_index value to the amount.

  sum += grades[loop_index] end 

Now you are looping on each element and summing all the elements of the array.

You can calculate the average value:

 average = sum/grades.length 

Now you write the result to stdout:

 puts average 

It was a rude syntax. Ruby-like you would do it like this:

 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 a comment by Marc-Andres:

You can also use inject to avoid determining the initial amount:

 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 
+20
source

From http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_integer.html#upto :

before int.upto( anInteger ) {| i | block } int.upto( anInteger ) {| i | block }

Iterate over the block , passing integer values ​​from int to and including anInteger .

 5.upto(10) { |i| print i, " " } 

gives:

 5 6 7 8 9 10 
+3
source

Upto executes the block specified once for each number from the original number "to" the argument passed. For instance:

 1.upto(10) {|x| puts x} 

will display numbers from 1 to 10.

+1
source

This is just another way to do a loop / iterator in Ruby. He says that he does this action n times, based on the fact that I am the first number in parens as a limit.

+1
source

An alternative that looks more like Ruby to me is

 require 'descriptive_statistics' grades=[88,99,73,56,87,64] sum = grades.sum average = grades.mean sd = grades.standard_deviation 

Of course, it depends on what you do.

0
source

My example would be like this:

 1.upto(5) { |i| puts "Countup: #{i}" } 

So, what are you really doing here, I say: I want to count from 1 to the number 5, which this part specifically says:

 1.upto(5) 

The last part of the code (block) simply prints an iteration of passing the counter from 1 to 5. This is the result that you might expect to see:

  Countup: 1 Countup: 2 Countup: 3 Countup: 4 Countup: 5 

Note This can be written in a different way if you use multilines:

 1.upto(5) do |i| puts "Countup: #{i}" end 

Hope this helps.

0
source

Source: https://habr.com/ru/post/910848/


All Articles