Created
March 27, 2010 11:36
-
-
Save mrorii/345966 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 Hoge | |
| def initialize(state, foo) | |
| @state = state | |
| @foo = foo | |
| end | |
| # セッター | |
| def state=(new_state) | |
| @state = new_state | |
| end | |
| # ゲッター | |
| def state | |
| @state | |
| end | |
| def print | |
| puts "@state is #{@state} and @foo is #{@foo}" | |
| end | |
| end | |
| h = Hoge.new("aho", 1) | |
| h.print #=> @state is aho and @foo is 1 | |
| h.state = "baka" # 8 行目の state= を呼ぶ | |
| h.print #=> @state is baka and @foo is 1 | |
| h.foo = "yukarin" #=> def foo=(...) を定義していないのでエラー | |
| h.foo #=> def foo を定義していないのでエラー | |
| puts h.state #=> baka | |
| # 12 行目で def state というゲッター(getter)を定義したので、 | |
| # @state の値を取得できる |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment