Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
一般來說,class static variable的accessor怎麼寫
# 如果你指的class static variable是@@a這種
# 我會這麼寫(至於多數人怎麼寫我就不清楚了...sorry = =")
class A
@@class_var = 'I am A'
def self.class_var
@@class_var
end
end
# 但是Ruby的設計,class static variable會被繼承
# 所以會出現這樣的狀況
class B < A
@@class_var = 'I am B'
end
p B.class_var # => "I am B"
p A.class_var # => "I am B"
# 因此,如果這不是你期望的行為
# 那用常數或者singleton會比較好
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.