About the
I felt that I could not accurately recognize the position of the spot of the times method and the meaning of the block variable other than counting from 0, so I tried several patterns and summarized it in my own way.
colors = ["Red", "Ao", "yellow"]
element_count = colors.length
i = 0
num = 0
element_count.times do |i|
i += 1
num += 1
end
# num =0 Define num here=>Even though the variable is not defined+Why the error occurred
#→ num in the times method=If it is set to 1, the output will be output, but it will be 0.(spot)
puts i #Output is 0
puts num #Output is 3
#------------------------------------------------------------------
#NameError when trying to output i defined by block variable i(Variable name i is not defined)。
#Block variables cannot be output.
#The block variable i and the variable i look the same, but they are completely different variables.
I would appreciate it if you could tell me any differences in recognition or supplements.
Recommended Posts