Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created October 5, 2011 04:48
Show Gist options
  • Select an option

  • Save pasberth/1263663 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/1263663 to your computer and use it in GitHub Desktop.
代入しても常に同じオブジェクトを参照したい
# -*- 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