Ruby Iterative Processing

Introduction

This will be a memo for learning. This time I would like to summarize various repetitive sentences.

for statement

In the "for" statement, the value is fetched in order from the object specified in advance and repeated.

for counter variable in repeat range do#do is optional
Iterative processing
end
Example) Pull-down list from 1 to 10 years old in HTML
puts "<select name='age'>"
for age in 1..10
  puts "<option>#{age}age</option"
end

puts "</select>"

while statement

Repeat the process as long as the conditional expression is true.

#Loop processing by while

#Initialize counter variable
while conditional expression do#do is optional
    #Iterative processing
    #Update counter variable
end
Example)
i=1.              //Initialization of counter variables
while i <=10      //Repeat 1 to 10
  puts i    #Iterative processing
  i=i+1            #Update counter variable
end

if statement (conditional branch)

if conditional expression
Processing when the conditional expression is satisfied
else
Processing when the conditional expression is not satisfied
end
number = 1
if number == 1
  puts "The number is 1"
else
  puts "The number is not 1"
end

[Execution result]

The number is 1

Multiple conditional expressions

if conditional expression
Processing when the conditional expression is satisfied
elsif conditional expression 2
Processing when conditional expression 2 is satisfied
else
Processing when none of the conditional expressions are satisfied
end

each statement

With each minute, the elements of the array can be taken out in order and processed.

Array.each do |Variable name|
  #The process you want to execute
end
names = ["Masato","Okachan","Saitou"]

names.each do |name|
  puts "Name is#{name}is"
end

[Execution result]

The name is Masato
The name is Okachan
The name is Saitou

Finally

There may be some mistakes, but I would appreciate it if you could point out that.

Recommended Posts

[Ruby] Iterative processing
Ruby Iterative Processing
Iterative processing
[Ruby ~ Iterative processing ~] Study memo 4
Memorandum (Ruby: Basic grammar: Iterative processing)
java iterative processing
[Beginner] Various iterative processing for Ruby arrays
Ruby conditional branch processing
Ruby About various iterative processes
[Swift] "for-in statement" about iterative processing
Memorandum (other conditional bifurcation, iterative processing)
Ruby learning 4
[Ruby / Refactoring] From Ruby iterative processing like Java and C language to Ruby-like iterative processing
[Ruby] Array
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
Ruby learning 3
[Ruby] Explanation for beginners of iterative processing with subscripts such as each_with_index!
Loop processing
Ruby setting 2
Ruby problem ⑦
Ruby learning 2
Iterative processing of Ruby using each method (find the sum from 1 to 10)
Omitting iterative processing using render's collection option
[Ruby] Block
Refactoring Ruby
Ruby learning 6
[Java ~ Conditional branching / Iterative processing ~] Study memo (3)
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
How to insert processing with any number of elements in iterative processing in Ruby
How to start a subscript from an arbitrary number in Ruby iterative processing
Escape processing when creating a URL in Ruby