Created
April 12, 2012 17:30
-
-
Save KL-7/2369386 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
def init_var | |
puts 'init var...' | |
'var value' | |
end | |
puts 'define class A' | |
class A | |
@@var = init_var | |
class << self | |
def use_var | |
puts @@var | |
end | |
end | |
end | |
puts 'class A defined' | |
puts 'using var' | |
A.use_var | |
puts | |
puts 'define class B' | |
class B | |
class << self | |
def use_var | |
puts var | |
end | |
def var | |
@var ||= init_var | |
end | |
end | |
end | |
puts 'class B defined' | |
puts 'using var' | |
B.use_var | |
__END__ | |
Output: | |
define class A | |
init var... | |
class A defined | |
using var | |
var value | |
define class B | |
class B defined | |
using var | |
init var... | |
var value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment