Skip to content

Instantly share code, notes, and snippets.

@speedmax
Created August 14, 2008 05:11
Show Gist options
  • Save speedmax/5372 to your computer and use it in GitHub Desktop.
Save speedmax/5372 to your computer and use it in GitHub Desktop.
-- global
people = {}
person = class {
function(self, name, age)
self.name, self.age = name, age
table.insert(people, self)
end
-- generic by default
get_name = function(self)
return self.name
end
-- class method
['class.find_by_name'] = function(name)
table.foreach(people, function(p)
if p.name:match(name) then
return p
end
end)
end
}
p1 = person('taylor luk', 26)
p2 = person('minhee', 99)
p3 = person('lee', 100)
print(p1.get_name()) -- prints 'taylor luk'
print(person.get_name(p2)) -- prints 'minhee'
-- class method example
print person.find_by_name('taylor').name -- prints 'taylor luk'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment