#!/usr/koeki/bin/ruby # coding: utf-8 price = Hash.new(0) open("fruits.csv", "r") do |f| while x = f.gets if /(.*),(\d+)/ =~ x then #$1=品名 $2=価格 (いずれも最初は文字列) price[$1] = $2.to_i # 価格を整数化 end end end puts "りんご、みかん、いちご、梨、のどの値段を知りたいですか?" what = gets.chomp! cost = price[what] printf("%sは1個%d円です\n", what, cost) if cost == 0 printf("っていうか%sは売ってないんで...\n", what) end