Created
May 2, 2016 16:13
-
-
Save Kumassy/68b2c670d6c8dc43df6cb64a1c52bfa9 to your computer and use it in GitHub Desktop.
Rubyでクラスインスタンス変数にインスタンスメソッドからアクセス
This file contains 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 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