Skip to content

Instantly share code, notes, and snippets.

@agnellvj
Forked from ahoward/a.rb
Created May 20, 2014 19:04
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 agnellvj/89a976b70a3ab1d2503f to your computer and use it in GitHub Desktop.
Save agnellvj/89a976b70a3ab1d2503f to your computer and use it in GitHub Desktop.
# 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 = :THIS_NEEDS_TO_INHERIT_THE_DEFAULT_VALUE_FROM_THE_PARENT_CLASS
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 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment