Created
October 5, 2011 04:48
-
-
Save pasberth/1263663 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
| # -*- coding: utf-8 -*- | |
| class Pointer < BasicObject | |
| def initialize value | |
| @value = value | |
| end | |
| def __value__ | |
| @value | |
| end | |
| def __value__= value | |
| @value = value | |
| end | |
| def method_missing funcname, *args, &blk | |
| @value.send(funcname, *args, &blk) | |
| end | |
| def inspect | |
| "#{@value.inspect} at #<Pointer:%x>" % __id__ | |
| end | |
| end | |
| ptr = Pointer.new("aaa") | |
| copy = ptr | |
| puts ptr | |
| puts copy | |
| ptr.__value__ = "bbb" | |
| #このように使っても常に同じ値 | |
| puts ptr | |
| puts copy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment