I have summarized the basic grammar of 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 #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
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
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 specifying multiple conditions, it is simpler to write in case than in elsif
#① 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
#① Create an empty hash
# {}
#(2) Hash creation to store key / value combinations
# {Key=>value}
[array or hash].each do |variable| #array is an array. hash is a hash.
#Process to be executed repeatedly
end
for variable in [array or hash] do #array is an array. hash is a hash.
#Process to be executed repeatedly
end
n.times do #n is the number of repetitions.
#Process to be executed repeatedly
end
while #Conditional expression
#Process to be executed repeatedly
end
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.
kaishishiki.step(#upper limit,Size that increases or decreases at once) #kaishishiki is the opening ceremony.
loop do
#Process to be executed repeatedly
end
Recommended Posts