The study period is now in the 11th week. Here's a look back at what I've learned and what I often make mistakes.
This time is "processing order". Although it is a basic and rudimentary content in the basics, there are many cases where an error occurred due to the wrong order of description, so I hope it will be helpful for those who are just starting to learn. Also, since the language currently being learned is "Ruby", the content will also be that of "Ruby".
The version of ruby you are using is as follows.
ruby 2.6.5
First of all, the order of program processing in the source is basically ** "line by line from top to bottom" *. This doesn't change when you create an app using complicated description or Ruby on Rails. ( When calling a method, conditional branching, or repeating processing, the line may jump from bottom to top, but it will be easier to understand if you are aware of the basic processing order.) Expressed in a simple program,
puts "1st line"
puts "2nd line"
puts "3rd line"
If you write, when you execute it on the console
1st line
2nd line
3rd line
It will be. With this description, the output result of the program puts "3rd line"
will not come to the 1st line. It would be hard if there was.
If conditional branching (if, while, etc.) or repetitive processing (times, each, etc.) overlaps with this, the order of processing tends to be sloppy. (Maybe I'm the only one ...)
Take, for example, the famous ** "FizzBuzz Problem" **.
FizzBuzz problem
1~Create a program that outputs the number 100.
When it is a multiple of 3, it is displayed as "Fizz", and when it is a multiple of 5, it is displayed as "Buzz".
Make a program to display.
However, when it is a multiple of 15, display "FizzBuzz".
An example of this answer is
#Correct code
def fizz_buzz
num = 0
while num <= 99
num += 1
if num % 15 == 0
puts "FizzBuzz"
elsif num % 3 == 0
puts "Fizz"
elsif num % 5 == 0
puts "Buzz"
else
puts num
end
end
end
puts fizz_buzz
The output result is as follows
Let's change the order of the conditional expressions in this program as follows.
#Wrong code
def fizz_buzz
num = 0
while num <= 99
num += 1
if num % 3 == 0 # num % 15 ==Change from the conditional expression of 0
puts "Fizz"
elsif num % 5 == 0
puts "Buzz"
elsif num % 15 == 0 #num % 3 ==Change from the conditional expression of 0
puts "FizzBuzz"
else
puts num
end
end
end
puts fizz_buzz
Here is the output result when described as above.
When it is a multiple of 15, "FizzBuzz" is not displayed but "Fizz" is displayed. The reason for this is "I don't understand the order of program processing."
Given the order of processing the wrong code above, For example, if num == 3,
#Wrong code
if num % 3 == 0 #This formula applies
puts "Fizz"
=> "Fizz"
When num == 5
#Wrong code
if num % 3 == 0 # num ==5 is not divisible by 3, so do the following
puts "Fizz"
elsif num % 5 == 0 # num ==5 is divisible by 5, so do this
puts "Buzz"
=> "Buzz"
If the problem num == 15
#Wrong code
if num % 3 == 0 # num ==15 is divisible by 3, so do this!
puts "Fizz"
elsif num % 5 == 0 # num ==15 was processed and no longer works.
puts "Buzz"
elsif num % 15 == 0 # num ==15 was processed and no longer works.
puts "FizzBuzz"
end
=> "Fizz"
It will be processed like this. So be careful about the order in which you write the program.
if num % 15 == 0 #First, it is determined whether num is divisible by 15.
puts "FizzBuzz" #If the conditional expression applies"FizzBuzz"If the display does not apply, go to the next
elsif num % 3 == 0 #Next, it is not divisible by 15, but it is judged whether it is divisible by 3.
puts "Fizz" #If the conditional expression applies"Fizz"Is displayed.
#If this also does not apply, go to the next
elsif num % 5 == 0 #It is not divisible by 3 or 15, but it is judged whether it is divisible by 5.
puts "Buzz" #If the conditional expression applies"Buzz"Is displayed.
#If this also does not apply, go to the next
else
puts num #If all the previous conditions are not met
end #Display num as it is
that's all. Although it is a basic thing, from my experience so far, there was a situation where I was in trouble because I did not have a solid grasp of these basics, so I would appreciate it if you could refer to it if you face a similar situation.
Recommended Posts