Created
December 27, 2012 17:21
-
-
Save vkgtaro/4390080 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ポケットモジュールを定義 | |
| module Pocket | |
| def insert_card(card) | |
| if @pocket | |
| raise "already exist in pocket" | |
| end | |
| @pocket = card | |
| end | |
| def pull_card | |
| if not @pocket | |
| return nil | |
| end | |
| card = @pocket | |
| @pocket = nil | |
| return card | |
| end | |
| end | |
| # Wallet に Pocket をつける | |
| class Wallet | |
| include Pocket | |
| def initialize | |
| @amount = 0 | |
| end | |
| def put(money) | |
| @amount += money | |
| end | |
| def pull_out(money) | |
| if @amount < money | |
| raise "be short of the needful" | |
| end | |
| @amount -= money | |
| end | |
| def get_balance | |
| return @amount | |
| end | |
| end | |
| wallet = Wallet.new | |
| wallet.put 10000 | |
| puts wallet.get_balance # => 10000 | |
| wallet.pull_out 500 | |
| puts wallet.get_balance # => 9500 | |
| wallet.insert_card "visa card" | |
| puts wallet.pull_card # => visa card |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment