to_○
x = 50
y = "3"
p x + y.to_i #to int
p x + y.to_f #to floo
scores = {keiichi:400, tomomi:2000}
p scores.to_a #to arry
p scores.to_h #to hassh
puts ("red", "blue")
puts %(hello) #You can use "" between OK
p ["red", "blue"]
p %W(red, blue) #OK
if
score = gets.to_i
if score > 80 then
puts "great!"
else if score > 60 then
puts "good"
else
puts "so so..."
end
case
#case
signal = gets.chomp #Chomp to remove the last newline code
case signal
when "red"
puts "stop!"
when "green"
puts "go!"
when "yello"
puts "caution!"
else
puts "wrong signal"
end
While
#while
i = 0
while i < 10 do
puts "#{i}: hello" # #{i}Is used to make it easier to see the number of times
i += 1 #i = i +Can be shortened to 1
end
Times
#times
10.times do |i| #i +=Same effect as 1
puts "hello"
end
for
for i in 15..20 do
p i
end
for color in ["red", "blue"] do
p color
end
for name, score in {taguchi:200, fkoji:400} do
puts "#{name}: #{score}"
end
for color in %W(red, bule) do
p color
end
each
(15..20).each do |i|
p i
end
["red", "blue"].each do |color|
p color
end
{taguchi:200, fkoji:400}.each do |name, score|
puts "#{name}: #{score}"
end
%W(red, bule).each do |color|
p color
end
Recommended Posts