2012年8月27日月曜日

Ruby 繰り返し処理

( ̄(エ) ̄)彡☆

timesメソッド
i = 1
10.times.do |i|
 print i, "\n"
end

uptoメソッド
1.upto(10) do |i|
 print i, "\n"
end

downtoメソッド
10.downto(1) do |i|
 print i, "\n"
end

eachメソッド railsでお馴染み(´▽`)
colors = ["red", "green", "blue"]
colors.each do |color|
 puts color
end

while文
i = 0
colors = ["red", "green", "blue"]
while i < colors.length
 puts colors[i]
 i += 1
end

for文
colors = ["red", "green", "blue"]
for color in colors
 puts color
end

0 件のコメント:

コメントを投稿