Ruby basics

[Basics of Ruby]

I have summarized the basic grammar of Ruby.

How to comment in Ruby

・ One-line comment (Java is //) </ strong>

Add # (hash mark or pound) to the line. #The string you want to comment on

・ Multi-line comment (In Java, the start of a comment is / * the end of a comment is * /) </ strong>

=begin The string you want to comment on The string you want to comment on The character you want to comment on  =end

if conditional branch

if #Condition 1
 #Processing to be executed when condition 1 is true
elsif #Condition 2
 #Processing to be executed when condition 2 is true
elsif #Condition 3
 #Processing to be executed when condition 3 is true
else
 #Processing to be executed in other cases
end
  • Be careful of the spelling elsif (Java used in business is else if)

unless conditional syntax

unless #Conditional expression
 #Processing to be executed when the conditional expression is false
else
 #Processing to be executed when the conditional expression is not false, that is, when it is true
end
  • If else is not required, it can be omitted.

case conditional syntax

case #Target object, target expression
when #Value 1
 #What to do if the value matches 1
when #Value 2
 #What to do if the value matches 2
when #Value 3
 #What to do if the value matches 3
else
 #What to do if it doesn't match any value
end
  • When can describe any number

When specifying multiple conditions, it is simpler to write in case than in elsif

Array (object that can store multiple data together)

#① Make an empty array
 # []

#(2) Create an array containing three elements
 # [Element 1,Element 2,Element 3]

The data in the array are in order. Extract data by specifying a subscript

Hash (an object that manages data with a combination of key and value)

#① Create an empty hash
 # {}

#(2) Hash creation to store key / value combinations
 # {Key=>value}

each iterative processing

[array or hash].each do |variable| #array is an array. hash is a hash.
 #Process to be executed repeatedly
end
  • Process while extracting the elements of the array in order from the beginning to the end

for Iterative processing

for variable in [array or hash] do #array is an array. hash is a hash.
 #Process to be executed repeatedly
end
  • In Ruby, when you can write with each, use each instead of for

times Iterative processing

n.times do #n is the number of repetitions.
 #Process to be executed repeatedly
end
  • Used when you want to repeat processing n times without using an array

while iterative processing

while #Conditional expression
 #Process to be executed repeatedly
end
  • Repeat the process while the conditional expression is true

upto, downto Iterative processing

upto
 #Used when executing some processing while increasing the numerical value by 1 from n to m
downto
 #Used to perform some processing while decreasing the number from n to m by 1.

step Iterative processing

kaishishiki.step(#upper limit,Size that increases or decreases at once) #kaishishiki is the opening ceremony.
  • Used when executing some processing while increasing the value by x from n to m Example) 1,3,5,7,9

loop Iterative processing

loop do
 #Process to be executed repeatedly
end
  • Used when you want to create an infinite loop

Recommended Posts