Skip to content

Instantly share code, notes, and snippets.

@mecampbellsoup
Last active February 2, 2020 20:34
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 mecampbellsoup/7c0dd50a23552e8f334e4a9f80ed61e7 to your computer and use it in GitHub Desktop.
Save mecampbellsoup/7c0dd50a23552e8f334e4a9f80ed61e7 to your computer and use it in GitHub Desktop.
class File:
file_count = 0
def __init__(self, name):
self.file_name = name
# self.file_count = 1
self.__class__.file_count += 1
f1 = File(__file__)
f2 = File(__file__)
print('f1.file_count = ', f1.file_count)
print('f2.file_count = ', f2.file_count)
print('File.file_count = ', File.file_count)
# f1.file_count = 2
# f2.file_count = 2
# File.file_count = 2
class File
@file_count = 0
class << self
attr_accessor :file_count
end
attr_reader :file_name, :file_count
def initialize(name)
@file_name = name
#@file_count = 1
self.class.file_count += 1
end
end
f1 = File.new(name=__FILE__)
f2 = File.new(name=__FILE__)
print "f1.file_count = ", f1.file_count, "\n"
print "f2.file_count = ", f2.file_count, "\n"
print "File.file_count = ", File.file_count, "\n"
# f1.file_count =
# f2.file_count =
# File.file_count = 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment