Created
December 27, 2012 17:21
-
-
Save vkgtaro/4390075 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
| # 普通の class 定義 | |
| class Wallet | |
| 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 クラスを継承 | |
| class Pocket < Wallet | |
| 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 | |
| # ポケット付き財布のインスタンスを生成 | |
| pocket_wallet = Pocket.new | |
| pocket_wallet.insert_card "visa card" | |
| puts pocket_wallet.pull_card # => visa card |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment