提出課題:04回目レポート 本文: c1222198 丸田ゆき 1.(1)-1まで実施しました。 2. #!/usr/koeki/bin/ruby # -*- coding: utf-8 -*- require 'kconv' soft = Hash.new while line = gets if /(\S+)\s+(\d+)\s+(\d+)\s+(\d+)/ =~ line b = $2.to_f / ($2.to_f + $3.to_f) soft[$1] = [$2.to_f,$3.to_f,$4,b] end end print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n" for team in soft.keys.sort {|a, b| soft[b][3] <=> soft[a][3]} result = sprintf("%-23s%4d%7d%7d%10.3f\n", team.toeuc.force_encoding("binary"), soft[team][0], soft[team][1], soft[team][2], soft[team][0]/(soft[team][0]+soft[team][1])).toutf8 puts result end 3../ruby2-3.rb softball.txt [~/program] --チーム名------------+-勝ち-+-負け-+-引分--+-勝率--- ドラゴンフライズ 77 51 8 0.602 アンツ 78 53 5 0.595 ブックウォームス 75 52 9 0.591 モスキートーズ 65 65 6 0.500 スネイルズ 52 82 2 0.388 クリケッツ 50 83 3 0.376 4. 授業内で作ったプログラムとの変更点は、10行目に b = $2.to_f / ($2.to_f + $3.to_f) という勝率を計算する変数bを定め、11行目のsoftの配列の3番目に入れていることです。 17行目で並び替えて表示させるときに {|a, b| soft[b][3] <=> soft[a][3]} のように配列の3番目の要素として勝率順を表示させました。 5.1行変えるだけでいいというヒントをいただきましたが、ちょっとわからなかったので新しく変数を設定するという大回りをしてしまいました。また、私はopenメソッドのところがちょっと怪しいので、もう一度復習する必要があるなと感じました。STDERR.print、STDERR.printfらへんを使うのかなとは思ったのですが、次回の解説でちょっと思い出したいです。 添付: ruby2-3.rb 添付: