Skip to content

Instantly share code, notes, and snippets.

@mrorii
Created March 9, 2010 12:31
Show Gist options
  • Save mrorii/326529 to your computer and use it in GitHub Desktop.
Save mrorii/326529 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
$have_title = {'BakaTesu' => [1,2,3],
'ZeroNo' => [1,2,3,4,5],
'Haruhi' => [1,2,3,4,5,6]}
class Ranobe
def buy_new_books(new_title)
new_title.each do |new_book|
title = new_book[0]
volume = new_book[1]
if $have_title.key?(title) then
# 同シリーズ名の作品を持っている(この例で言えば「BakaTesu」)
if $have_title[title].include?(volume) then
puts "間違って" + title + " " + volume.to_s + "巻をまた買ってしまった"
else
$have_title[title].push(volume)
puts title + " " + volume.to_s + "巻を購入した"
end
else
# 同シリーズ名の作品を持っていない(この例で言えば「Hantuki」)
# $have_title に新規追加
$have_title[title] = [volume]
puts title + "シリーズを新たに購入した"
end
end
end
end
bought = [['BakaTesu', 4],
['HanTuki', 1],
['Haruhi', 3]] # ハルヒ3巻は持っているが間違って買ってしまった
myobj = Ranobe.new
myobj.buy_new_books(bought)
puts "-" * 40
p $have_title # p はデバッグ用メソッド
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment