3
3

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 5 years have passed since last update.

【Ruby】繰り返し文each do

Last updated at Posted at 2018-06-28

基礎中の基礎をおさらい

配列って覚えてますか?


names = ["Uemoto", "Tanaka", "Kokubo", "Abe", "Dohbayashi", "Maru", "Iwamoto"]

この配列の要素に対して同じ処理を繰り返し行いたい時に、each文を用います


names = ["Uemoto", "Tanaka", "Kokubo", "Abe", "Dohbayashi", "Maru", "Iwamoto"]

puts "#{names[0]}はカープ選手です"
puts "#{names[1]}はカープ選手です"
puts "#{names[2]}はカープ選手です"
puts "#{names[3]}はカープ選手です"
puts "#{names[4]}はカープ選手です"
puts "#{names[5]}はカープ選手です"
puts "#{names[6]}はカープ選手です"

配列.each do |変数|


names = ["Uemoto", "Tanaka", "Kokubo", "Abe", "Dohbayashi", "Maru", "Iwamoto"]

names.each do |name|
puts "#{name}はカープ選手です"
end

Uemotoはカープ選手です
Tanakaはカープ選手です
Kokuboはカープ選手です
Abeはカープ選手です
Dohbayashiはカープ選手です
Maruはカープ選手です
Iwamotoはカープ選手です

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?