[RAILS] Iterative processing of Ruby using each method (find the sum from 1 to 10)


![Screenshot 2020-08-31 15.49.56.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/694973/7de31e7d-4555-29a3-20ac -21aa281f02fc.png)

Model answer

sum = 0

10.times do |i|
  sum = sum + i + 1

My answer

sum = 0
numbers = [1,2,3,4,5,6,7,8,9,10]
numbers.each do |n|
  sum += n


I use the times method in the model answer, whereas I use the each method to repeatedly calculate the array. The common point is that the value sum is defined outside the block, and the processing to be performed for that sum is described in the block from do to end. But writing everything from 1 to 10 isn't smart. There is a range object that represents a range of consecutive values such as "1 to 10". Create a range object using ... or .. like this:

Range object

1 ... 10 (including 10) 1..10 (not including 10) You can use the to_a method on this range object to create a contiguous array of values

to_a method

![Screenshot 2020-08-31 16.24.21.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/694973/da10d3d6-3cdf-7c2b-17f5 -93c9b25ea192.png) You now have a continuous array of up to 10! You can also create an array in the same way by using * and a range object in []. This is called splat expansion. It will be as follows. スクリーンショット 2020-08-31 16.29.32.png

If you put together the above and make the answer of the example again, it will be as follows

sum = 0
numbers = [*1..10]
numbers.each do |n|
  sum += n

I was able to make it much cleaner from the beginning! You can also make it even more compact by using the each method directly on the range object. At the same time, instead of using do ... end as the block notation, we will use {}.

sum = 0
 (1..10).each{ |n| sum += n  }


Introduced how to make iterative calculation compact by using range object and to_a method in each method. When it comes to programming, not only is the answer given, but it's not easy for the viewer to understand, and it's easier to write code there. I would like to continue to output with an awareness of how easy it is to write code!


Introduction to Ruby for professionals From language specifications to test-driven development / debugging techniques Software Design plus Kindle version Junichi Ito () https://www.amazon.co.jp/dp/B077Q8BXHC/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1

Recommended Posts

Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Ruby] From the basics to the inject method
How to find the cause of the Ruby error
[Android, Java] Method to find the elapsed date from two dates
Ruby from the perspective of other languages
Find the difference from a multiple of 10
[Ruby / Refactoring] From Ruby iterative processing like Java and C language to Ruby-like iterative processing
[Ruby On Rails] How to search the contents of params using include?
How to insert processing with any number of elements in iterative processing in Ruby
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
Output the sum of each name and its contents from a multiple array
If it is Ruby, it is efficient to make it a method and stock the processing.
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
[Ruby] Iterative processing
The story of introducing Ajax communication to ruby
Output of how to use the slice method
[Ruby] How to calculate the total amount using the initialize method and class variables
[Ruby] Code to display the day of the week
Ruby Iterative Processing
[JDBC ③] I tried to input from the main method using placeholders and arguments.
How to start a subscript from an arbitrary number in Ruby iterative processing
[Ruby basics] How to use the slice method
[Ruby] Cut out a string using the slice method
From the introduction of devise to the creation of the users table
[Ruby] Implement error handling / iteration processing using while method
How to write Scala from the perspective of Java
The story of migrating from Paperclip to Active Storage
[Ruby] Method to easily get the receiver type .class
I want to call the main method using reflection
Are you using the default method of the interface properly?
The first step to using Xib instead of StoryBoard
Try using the query attribute of Ruby on Rails
Sum from Java_1 to 100
definition of ruby method
From Java to Ruby !!
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
The story of raising Spring Boot from 1.5 series to 2.1 series part2
[Ruby] Count an even number in an array using the even? Method
[Ruby] Questions and verification about the number of method arguments
I want to expand the clickable part of the link_to method
[Ruby] Find a permutation consisting of 0 to 9 at high speed.
How to get the longest information from Twitter as of 12/12/2016
[Ruby] Creating code using the concept of classes and instances
[Ruby] Display today's day of the week using Date class
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I tried to summarize the basic grammar of Ruby briefly
[Ruby] How to retrieve the contents of a double hash
The story of pushing Java to Heroku using the BitBucket pipeline
[Ruby] I want to reverse the order of the hash table
[Apache Tomcat] The story of using Apache OpenWebBeans to enable CDI
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~
Try using Cocoa from Ruby
[Ruby ~ Iterative processing ~] Study memo 4
I tried to solve the problem of "multi-stage selection" with Ruby
[Java] Try to solve the Fizz Buzz problem using recursive processing
I want to understand the flow of Spring processing request parameters
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
How to get the contents of Map using for statement Memorandum
[Rails] How to change the page title of the browser for each page
Method to add the number of years and get the end of the month