第 10 回 ことおぼえ レポート課題 氏名:豊田純平 学籍番号:c1101626 語学クラス:英語6 コース(系):政策マネジメントコース 1.筆記問題 pstore機能をつかう事で、ホームページの訪問者などの第三者もファイルへの書 き込みが可能となる。その利用方法は、cgiを用いたプログラムの入っているフォ ルダのcgirubyの下にpstoreというディレクトリを作成し、chmod o+w pstoreと する。そしてtouch データ名.dbとし、次にchmod o+w データ名.dbでデータファ イルを作成する。 書き込んだデータを取り出し、有効にするにはchmod o+w プログラム.rbとやっ ておく。プログラムなので、chmodを忘れないようにする。 2.プログラム 私は課題Gのオンライン授業アンケートのプログラムを作成した。
#!/usr/bin/env ruby require 'cgi' #cgiを使う宣言をする c = CGI.new("html4") print"Content-type: text/html; charset=EUC-JP\n\n" data = c["likedes"] #cgi変数を配列に代入 dat = c["arunai"] da = c["tatutatanai"] d = c["ganbarun"] ziyu = c["ziyu"] like = 0 #初期値を0と規定する des = 0 aru = 0 nai = 0 tatu = 0 tatanai = 0 reru = 0 renai = 0 open("r2_cgi_best.dat","r+") do |haru| # open-endの中でwhile-endを使っ てファイル内の値をとりだし、集 計数をlikeとdesに代入する。 while line = haru.gets if /like/ =~ line # 集計結果の加算と、ファイル への書込みを行っている。 like += 1 elsif /des/ =~ line des += 1 end end if data == "like" like += 1 haru.printf("%s\n",data) elsif data == "des" des += 1 haru.printf("%s\n",data) end end open("r2_cgi_best.dat","r+") do |haru| while line = haru.gets if /aru/ =~ line aru += 1 elsif /nai/ =~ line nai += 1 end end if dat == "aru" aru += 1 haru.printf("%s\n",dat) elsif dat == "nai" nai += 1 haru.printf("%s\n",dat) end end open("r2_cgi_best.dat","r+") do |haru| while line = haru.gets if /tatu/ =~ line tatu += 1 elsif /tatanai/ =~ line tatanai += 1 end end if da == "tatu" tatu += 1 haru.printf("%s\n",da) elsif da == "tatanai" tatanai += 1 haru.printf("%s\n",da) end end open("r2_cgi_best.dat","r+") do |haru| while line = haru.gets if /reru/ =~ line reru += 1 elsif /renai/ =~ line renai += 1 end end if d == "reru" reru += 1 haru.printf("%s\n",d) elsif d == "renai" renai += 1 haru.printf("%s\n",d) end end print"好き | "
1.upto(like) do |i|
if i%20 == 0 # 20個ずつ折り返すようにしている
print"★ " # 1つの結果を★1つで表している else print"★" end end printf(" %d |
嫌い | "
1.upto(des) do |i|
if i%20 == 0
print"★ " else print"★" end end printf(" %d |
感じられる | "
1.upto(aru) do |i|
if i%20 == 0
print"★ " else print"★" end end printf(" %d |
感じられない | "
1.upto(nai) do |i|
if i%20 == 0
print"★ " else print"★" end end printf(" %d |
はい | "
1.upto(tatu) do |i|
if i%20 == 0
print"★ " else print"★" end end printf(" %d |
いいえ | "
1.upto(tatanai) do |i|
if i%20 == 0
print"★ " else print"★" end end printf(" %d |
いけそう | "
1.upto(reru) do |i|
if i%20 == 0
print"★ " else print"★" end end printf(" %d |
きびしい | "
1.upto(renai) do |i|
if i%20 == 0
print"★ " else print"★" end end printf(" %d |
%s
\n",ziyu) #自由記述の結果を出力する。 print"\n" print"\n" アドレスはこちらである。 http://roy/~c110162/advertise2/rubycgi/r2_cgi_best.html 4. 私はc106034さんのcgiスクリプトを用いた作品にひかれた。radioボタンによっ て選択した品に応じて画像が表示されるのは工夫がしてあるなと関心させられ た。私のあのようなプログラムを作成してみたいものだ。 http://roy.e.koeki-u.ac.jp/~c106034/rubycgi/purezent.html 5.参考文献 神田教授の授業ページ http://roy/~naoya/rubyII/index.html