'싸우자슉슉'에 해당되는 글 1건

  1. 2009.03.10 Ruby로 구구단을 찍는 9가지 방법 by sloth_chord
# 이 코드들은 농담들임다....
def print_multi(i, j)
  puts "#{i} * #{j} = #{i * j}"
end

def use_each
  (2 .. 9).each {|i| (1 .. 9).each {|j| print_multi(i, j)}; print"\n"}
end

#use_each

def use_upto
  2.upto(9) do |i|
    1.upto(9) do |j| print_multi(i, j) end
    print "\n"
  end
end

#use_upto

def use_times
  8.times do |i|
    9.times do |j| print_multi(i + 2, j + 1) end
    print "\n"
  end
end

#use_times

def use_step
  2.step(9, 1)  do |i|
    1.step(9, 1) do |j| print_multi(i, j) end
    print "\n"
  end
end


#use_step

def use_while
  i, j = 2, 1

  while i <= 9
    while j <= 9 do print_multi(i, j); j += 1 end
    print "\n"; i += 1; j = 1
  end
end

#use_while

def use_until
  i, j = 2, 1

  until i > 9
    until j > 9 do print_multi(i, j); j += 1 end
    print "\n"; i += 1; j = 1
  end
end

#use_until

def use_for
  for i in (2 .. 9)
    for j in (1 .. 9) do print_multi(i, j) end
    print "\n"
  end
end

#use_for

def use_infinite
  i, j = 2, 1

  while true
    break if i > 9
    while true
      break if j > 9
      print_multi(i, j); j += 1
    end
    print "\n"; i += 1; j = 1
  end
end

#use_infinite



def use_rec(i = 2, j = 1)
  return if i > 9
  j > 9 ? (print "\n"; use_rec(i + 1, 1)) : (print_multi(i, j); use_rec(i, j + 1))
end

#use_rec

Posted by sloth_chord

댓글을 달아 주세요