Skip to content

Instantly share code, notes, and snippets.

@mrorii
Created March 9, 2010 11:50
Show Gist options
  • Save mrorii/326513 to your computer and use it in GitHub Desktop.
Save mrorii/326513 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
$have_title = {'Bob' => [1,2,3],
'Michael' => [1,2,3,4,5],
'Tom' => [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
# 同シリーズ名の作品を持っている(上の例で言えば「Bob」)
if $have_title[title].include?(volume) then
puts "同じものを買ってきてしまった"
else
$have_title[title].push(volume)
end
else
# 同シリーズ名の作品を持っていない(上の例で言えば「Matt」)
# $have_title に新規追加
$have_title[title] = [volume]
end
end
end
end
bought = [['Bob', 4],
['Matt', 1]]
myobj = Ranobe.new
myobj.buy_new_books(bought)
p $have_title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment