-
-
Save cookrn/062173b60884af5b3a42 to your computer and use it in GitHub Desktop.
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
# fork it | |
# | |
# make it print all true with only ONE LINE OF CODE | |
class A | |
def A.foo | |
@foo ||= ( | |
if self == A | |
'42.0' | |
else | |
# inherited = :FIXME | |
# inherited = '42.0' | |
superclass.foo.dup | |
end | |
) | |
end | |
def A.foo=(foo) | |
@foo = foo | |
end | |
end | |
class B < A | |
end | |
class C < B | |
end | |
p( A.foo == B.foo ) | |
p( A.foo.object_id != B.foo.object_id ) | |
p( B.foo == C.foo ) | |
p( B.foo.object_id != C.foo.object_id ) | |
p( A.foo == C.foo ) | |
p( A.foo.object_id != C.foo.object_id ) | |
A.foo = 1 | |
B.foo = 2 | |
C.foo = 3 | |
p( A.foo != B.foo ) | |
p( B.foo != C.foo ) | |
p( A.foo != C.foo ) | |
module M; end | |
A.send(:extend, M) | |
B.send(:extend, M) | |
C.send(:extend, M) | |
p( A.foo == 1 ) | |
p( B.foo == 2 ) | |
p( C.foo == 3 ) |
Is that right then? You just make a copy from the superclass and return it if it isn't the parent class?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
heh https://gist.github.com/cookrn/062173b60884af5b3a42#file-a-rb-L12 is cheating.
the point is that the subclasses in inherit the value from the parent
think 'class inheritable attributes'