Skip to content

Instantly share code, notes, and snippets.

@KL-7
Created April 12, 2012 17:30
Show Gist options
  • Save KL-7/2369386 to your computer and use it in GitHub Desktop.
Save KL-7/2369386 to your computer and use it in GitHub Desktop.
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