Skip to content

Instantly share code, notes, and snippets.

@Kumassy
Created May 2, 2016 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kumassy/68b2c670d6c8dc43df6cb64a1c52bfa9 to your computer and use it in GitHub Desktop.
Save Kumassy/68b2c670d6c8dc43df6cb64a1c52bfa9 to your computer and use it in GitHub Desktop.
Rubyでクラスインスタンス変数にインスタンスメソッドからアクセス
class C
class << self
# proxy methods
def class_instance_variable
@class_instance_variable
end
def class_instance_variable=(other)
@class_instance_variable = other
end
end
@class_instance_variable = 12
def get_class_instance_variable
self.class.class_instance_variable
end
def set_class_instance_variable(other)
self.class.class_instance_variable = other
end
end
a = C.new
p a.get_class_instance_variable # => 12
b = C.new
p b.get_class_instance_variable # => 12
a.set_class_instance_variable(-44)
p a.get_class_instance_variable # => -44
p b.get_class_instance_variable # => -44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment