Consideration about the times method

About the

times method

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

Consideration about the times method
Output about the method # 2
About the length method
About the authenticate method.
About the map method
About the ancestors method
About the to_s method.
Output about the method Part 1
about the where method (rails)
About the role of the initialize method
About the package
About No Method Error
About the StringBuilder class
Commentary: About the interface
About the asset pipeline
About the function double-java
About Java method binding
About the ternary operator
About method splitting (Java)
About the Kernel module
[Output] About the database
About the [ruby] operator
About the handling of Null
About specifying the JAXRS path
Note the StringUtils # containsAny method
What is the pluck method?
A note about the scope
[Ruby] Questions and verification about the number of method arguments
About the description of Docker-compose.yml
About the File :: Stat class
[Java Silver] About equals method
Understand the helper method form_with
About the same and equivalent
About the Android life cycle
About the explanation about functional type
Consideration about classes and instances
About the programming language Crystal
About Rails scraping method Mechanize
What is the initialize method?
How to use the link_to method
About the behavior of ruby Hash # ==
Simulate the simplex method with GUI
About the language to be learned
How to use the include? method
How to use the form_with method
[Rails] About the Punk List function
About the equals () and hashcode () methods
About the symbol <%%> in Rails erb
About the information sharing app band
Java comparison using the compareTo () method
Resilience4j TimeLimiter times out method invocation
About the current development environment (Java 8)
I tried to explain the method
Consideration on the 2017 Java Persistence Framework (1)
A murmur about the utility class
[Rails] About helper method form_with [Basic]
Think about the 7 rules of Optional
[Rails 6] destroy using the resources method
Was that so, the user_signed_in? method
Consideration about Rails and Clean Architecture
Summary about the introduction of Device