Skip to content

Instantly share code, notes, and snippets.

@guilherme
Created March 28, 2011 01:51
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 guilherme/889866 to your computer and use it in GitHub Desktop.
Save guilherme/889866 to your computer and use it in GitHub Desktop.
A class that make an attribute unique.
class A
attr_accessor :name
@@a_classes = []
private_class_method :new
def A.find_or_create(name)
@@a_classes.each do |klass|
if klass.name == name
return klass
end
end
new_klass = new name
@@a_classes.push(new_klass)
new_klass
end
def initialize(name)
self.name = name
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment