表題の通りRuby勉強会に参加してきた。
okkezさんとチカホリさんによる「Ruby初心者向けレッスン第17回」の演習。
時間内にできるだけ解こうとした為、ものすごく汚いcodeになってしまった。
とりあえず晒す。
okkezさんとチカホリさんによる「Ruby初心者向けレッスン第17回」の演習。
時間内にできるだけ解こうとした為、ものすごく汚いcodeになってしまった。
とりあえず晒す。
九九の表
s=""
printf " |"
s+= "----+"
(1..9).to_a.each do |y|
printf "%3d",y.to_s
s+="---"
end
puts ""
puts s
(1..9).to_a.each do |x|
printf "%3d |",x.to_s
(1..9).to_a.each do |y|
printf "%3d",(x*y).to_s
end
puts ""
end
printf " |"
s+= "----+"
(1..9).to_a.each do |y|
printf "%3d",y.to_s
s+="---"
end
puts ""
puts s
(1..9).to_a.each do |x|
printf "%3d |",x.to_s
(1..9).to_a.each do |y|
printf "%3d",(x*y).to_s
end
puts ""
end
100マス計算
op="+"
xx=(1..9).sort_by{rand}
yy=(1..9).sort_by{rand}
s=""
printf " "+op+" |"
s+= "----+"
xx.to_a.each do |x|
printf "%3d",x.to_s
s+="---"
end
puts ""
puts s
yy.to_a.each do |y|
printf "%3d |",y.to_s
xx.to_a.each do |x|
printf "%3d",(x.send(op,y)).to_s
end
puts ""
end
xx=(1..9).sort_by{rand}
yy=(1..9).sort_by{rand}
s=""
printf " "+op+" |"
s+= "----+"
xx.to_a.each do |x|
printf "%3d",x.to_s
s+="---"
end
puts ""
puts s
yy.to_a.each do |y|
printf "%3d |",y.to_s
xx.to_a.each do |x|
printf "%3d",(x.send(op,y)).to_s
end
puts ""
end
ここまでで時間切れ。
帰ってから100マス計算をリファクタリング。というか再実装。
帰ってから100マス計算をリファクタリング。というか再実装。
100マス計算:再実装版
operator = ARGV[0].to_s
unless (/^[-+*\/%]$/ =~ operator)
puts "wrong parameter: operator is only +-*/%"
exit
end
x = (1..9).sort_by{rand}
y = (1..9).sort_by{rand}
def display100Math x,y,operator,display
digit = 2
separate = " "
vertical_line = "|"
horizontal_line = "-"
cross_line = "+"
print separate * (digit - operator.size)
print operator
print separate
print vertical_line
x.to_a.each do |xx|
print separate
print separate * (digit - xx.to_s.size)
print xx
end
puts ""
print horizontal_line * digit
print horizontal_line
print cross_line
x.size.times do
print horizontal_line
print horizontal_line * digit
end
puts ""
y.to_a.each do |yy|
print separate * (digit - yy.to_s.size)
print yy
print separate
print vertical_line
if display
x.to_a.each do |xx|
answer = yy.send(operator,xx)
print separate
print separate * (digit - answer.to_s.size)
print answer
end
end
puts ""
end
end
display100Math x,y,operator,false
puts
display100Math x,y,operator,true
unless (/^[-+*\/%]$/ =~ operator)
puts "wrong parameter: operator is only +-*/%"
exit
end
x = (1..9).sort_by{rand}
y = (1..9).sort_by{rand}
def display100Math x,y,operator,display
digit = 2
separate = " "
vertical_line = "|"
horizontal_line = "-"
cross_line = "+"
print separate * (digit - operator.size)
print operator
print separate
print vertical_line
x.to_a.each do |xx|
print separate
print separate * (digit - xx.to_s.size)
print xx
end
puts ""
print horizontal_line * digit
print horizontal_line
print cross_line
x.size.times do
print horizontal_line
print horizontal_line * digit
end
puts ""
y.to_a.each do |yy|
print separate * (digit - yy.to_s.size)
print yy
print separate
print vertical_line
if display
x.to_a.each do |xx|
answer = yy.send(operator,xx)
print separate
print separate * (digit - answer.to_s.size)
print answer
end
end
puts ""
end
end
display100Math x,y,operator,false
puts
display100Math x,y,operator,true
誰か添削してぇ〜〜。