概要
Integer#times
詳細
self 回だけ繰り返します。 self が正の整数でない場合は何もしない。
またブロックパラメータには 0 から (self - 1) までの数値が渡される。
サンプルコード
# encoding: utf-8 5.times {puts "hoge"} 0.times {puts "@@@@"} 5.times { |i| puts "#{i}times"} # short coder用 ## 0から始める場合 (0..4).each{|i|print"#{i}"} # 27byte puts 5.times{|i|print"#{i}"} # 23byte puts ## 1から始める場合 (1..5).each{|i|print"#{i}"} # 27byte puts 5.times{|i|print"#{i+1}"} # 25byte
出力
hoge hoge hoge hoge hoge 0times 1times 2times 3times 4times 01234 01234 12345 12345