[Ruby] Basic key to be strong in refactoring

It's an embarrassing story, but until recently, when I was told to "write beautiful code", I was conditioned reflexively thinking "I shouldn't have an error." However, as I understood the term refactoring and touched on some notations and methods, I began to think that beautiful code was beautiful (a little exaggeration: sweat_smile :), so I will give you an example of basic refactoring. I would like to introduce you.

Input and output to be set this time

Output the sum of consecutive arrays from 1 to 5. (The answer is 15) I would like to challenge how to describe this neatly and derive it.

Part 1: The push

p 1 + 2 + 3 + 4 + 5

… A program or a calculator? The simplest description of the level. Of course, it is not possible to deal with the case of calculating the total of numbers from 1 to 100. I want to make it expandable.

Part 2: Use arrays and each statements

numbers = [1,2,3,4,5]
sum = 0 #Assign 0 to the variable sum
 numbers.each do |n|
   sum += n #Repeatedly add the block variable n to the variable sum
 end
p sum

It may be the first code you learn in each statement. Even in programming, I get the feeling that "it is troublesome to write the elements of an array as 1,2,3 ..." and "I want to make each statement compact over several lines".

Part 3: Simple with range object and {} block notation

numbers = (1..5) #Creating a range object. 1~Means an array of consecutive values up to 5.
sum = 0
  numbers.each {|n| sum += n } #{}Put it on one line using the block notation of.
p sum

__ (first value .. last value) __ can represent a range of values.

Part 4: Use the inject method instead of each

numbers = (1..5)
 p sum = numbers.inject(0) {|n, s| n + s}
  #The inject method works according to the following flow.
  #The first argument of the method goes into the first argument of the block.
  #Each element of the array is entered in order in the second argument of the block.
  #The return value of the block is inherited by the first argument of the block.
  #When the iterative process is completed, the return value of the block becomes the return value of the inject method.

By using the inject method, we were able to handle multiple arguments and fit them in two lines.

In the future, I will continue to read the "Readable Code" that I heard as a good book, and I will do my best to write a beautiful code that will show my heart. If you have an opinion that "it's more concise to write like this", please let us know!

Recommended Posts

[Ruby] Basic key to be strong in refactoring
[Rails] How to write user_id (foreign key) in strong parameter
How to iterate infinitely in Ruby
Try to implement Yubaba in Ruby
How to install Bootstrap in Ruby
Be careful when omitting return in Ruby
How to get date data in Ruby
Introduction to Ruby basic grammar with yakiniku
Refactoring Ruby
Convert numbers to Roman numerals in Ruby
Refactoring Ruby
Refactoring Ruby
I want to use arrow notation in Ruby
CORBA seems to be removed in Java SE 11. .. ..
There seems to be no else-if in java
Try to get redmine API key with ruby
I want to be eventually even in kotlin
Site summary to be helpful in Minecraft modding 1.12.2
[Ruby on Rails] How to install Bootstrap in Rails
How to build the simplest blockchain in Ruby
How to implement Pagination in GraphQL (for ruby)
I want to get the value in Ruby
Introduction to Ruby 2
Ruby basic terms
Class in Ruby
Heavy in Ruby! ??
Refactoring in JUnit
[Ruby] How to use standard output in conditional branching
[Ruby on Rails] How to write enum in Japanese
Basic functional interface that can be understood in 3 minutes
Basic rules to be aware of to write easy-to-read code
Converting TSV files to CSV files (with BOM) in Ruby
[Ruby On Rails] How to reset DB in Heroku
How to launch another command in a Ruby program
[Ruby] Difficulty in refactoring logical operators (accuracy and readability)
How to handle TSV files and CSV files in Ruby
How to write an external reference key in FactoryBot
Be able to write Hamcrest Matchers in lambda expressions
How to resolve SSL_connect error in PayPal Ruby SDK