LoginSignup
0
0

More than 5 years have passed since last update.

【Ruby】コーディングメモ(ループ処理など)

Posted at

環境

ruby 2.4.1p111

ループ処理(for in do end)

sample.rb
for i in 0..10 do
    puts "ループ変数=#{i}"
end

#(出力結果)
ループ変数=0
ループ変数=1
ループ変数=2
ループ変数=3
ループ変数=4
ループ変数=5
ループ変数=6
ループ変数=7
ループ変数=8
ループ変数=9
ループ変数=10

配列ループ処理

sample.rb
employees = ["Yamada", "Tanaka", "Satoh"]
for emp in employees do
  print(emp + "¥n")
end

#(出力結果)
Yamada
Tanaka
Satoh

ループ処理(while do end)

sample.rb
i = 0
while i <= 10 do
    puts "ループ変数=#{i}"
    i+=1
end

#(出力結果)
ループ変数=0
ループ変数=1
ループ変数=2
ループ変数=3
ループ変数=4
ループ変数=5
ループ変数=6
ループ変数=7
ループ変数=8
ループ変数=9
ループ変数=10
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0