0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rubyの繰り返し処理をサンプルコードでまとめ

Last updated at Posted at 2021-09-26

繰り返し処理はEnumerableモジュールに宣言されたメソッド。
詳細は以下を参照。

配列系

each 配列の値を1つずつ取り出す

配列

loop.rb
week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

week_str = ''
week.each do |date|
    week_str += date
end
p week_str # -> SundayMondayTuesdayWednesdayThursdayFridaySaturday

ハッシュ
ハッシュの時、keyとvalueを返す。

loop.rb
country_code = {
    'japan' => 'JP',
    'australia' => 'AU',
    'canada' => 'CA'
}

country_code.each do |key, value|
    puts "#{key} : #{value}" 
end

# ->
# japan : JP
# australia : AU
# canada : CA

each_with_index 配列の値を1つずつ取り出し、第2ブロック引数にインデックスを渡す

loop.rb
week_str = ''
week.each_with_index do |date, i|
    week_str += "#{i.to_s}:#{date} "
end
p week_str # -> "0:Sunday 1:Monday 2:Tuesday 3:Wednesday 4:Thursday 5:Friday 6:Saturday "

map 配列の値を1つずつ取り出し、ブロックの式を実行した後の新しい配列を返す

loop.rb
good_week = week.map { |date| date += ' is good day.'}
p good_week # > ["Sunday is good day.", "Monday is good day.", "Tuesday is good day.", "Wednesday is good day.", "Thursday is good day.", "Friday is good day.", "Saturday is good day."]

select 配列の値を1つずつ取り出し、ブロックの条件が真の値のみの新しい配列を返す

loop.rb
sufix_sday_week = week.select { |date| date.include?('sday') }
p sufix_sday_week # > ["Tuesday", "Wednesday", "Thursday"]

reject 配列の値を1つずつ取り出し、ブロックの条件が偽の値のみの新しい配列を返す

loop.rb
sufix_not_sday_week = week.reject { |date| date.include?('sday') }
p sufix_not_sday_week # > ["Sunday", "Monday", "Friday", "Saturday"]

find 配列の値を1つずつ取り出し、ブロックの条件が最初に真になった値を返す

loop.rb
sufix_sday_date = week.find { |date| date.include?('sday') }
p sufix_sday_date # > "Tuesday"

inject 配列の値を1つずつ取り出し、たたみこみ演算を行う。

loop.rb
num = [1, 2, 3, 4, 5]
sum = num.inject(10) {|result, n| result + n}
p sum

delete_if 配列の値を1つずつ取り出し、ブロックの条件が真のものを配列から削除する

loop.rb
sample_week_1 = week.dup
sample_week_1.delete_if do |date|
    date.include?('sday')
end
p sample_week_1 # -> ["Sunday", "Monday", "Friday", "Saturday"]

with_index 繰り返し処理の後に繋げると、インデックスを第2ブロック引数に渡す

loop.rb
good_week = week.map.with_index { |date, i| date += " is good day. Day#{i}"}
p good_week # > ["Sunday is good day. Day0", "Monday is good day. Day1", "Tuesday is good day. Day2", "Wednesday is good day. Day3", "Thursday is good day. Day4", "Friday is good day. Day5", "Saturday is good day. Day6"]

数値系

times n回繰り返したい処理を行う

loop.rb
10.times{|n| puts n} # > 0 1 2 3 4 5 6 7 8 9

upto XからYまで1つずつ増やしながら処理を行う

X.upto(Y)

loop.rb
a = []
5.upto(10) { |n| a << n}
p a # > [5, 6, 7, 8, 9, 10]

downto XからYまで1つずつ減らしながら処理を行う

X.downto(Y)

loop.rb
a = []
10.downto(5) { |n| a << n}
p a # > [10, 9, 8, 7, 6, 5]

step XからYまでZずつ値を増やしながら処理を行う

X.step(Y, Z)

loop.rb
a = []
10.step(30, 2) { |n| a << n}
p a # > [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]

a = []
30.step(10, -2) { |n| a << n}
p a # > [30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10]

汎用系

while 条件が真であれば処理を続ける

loop.rb
a = [1, 2, 3, 4, 5]
i = 0
while a.size > 0
    a.shift # shiftは配列の先頭を削除する
    i += 1
    p i
end
# > 1, 2, 3, 4, 5

until 条件が偽であれば処理を続ける

loop.rb
a = [1, 2, 3, 4, 5]
i = 0
until a.size == 0
    a.shift # shiftは配列の先頭を削除する
    i += 1
    p i
end
# > 1, 2, 3, 4, 5

loop 無限ループを行う

無限ループを行う。
break後に条件を記述し、ループを抜ける。

loop.rb
sum = 0
loop do
    sum += 24
    break if sum > 100
end
p sum # > 120
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?