Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created March 6, 2015 22:51
Show Gist options
  • Save mayfer/6c03d91bca62f780532b to your computer and use it in GitHub Desktop.
Save mayfer/6c03d91bca62f780532b to your computer and use it in GitHub Desktop.
Introduction to Objects and Classes
class Person
attr_accessor(:name, :email)
# class variable
@@emails = []
@@population = 0
def initialize(name, email)
# instance variable
@name = name
@@population += 1
puts "Created new person #{name}, population now #{Person.population}"
if valid_email(email)
@email = email
@@emails << email
end
end
# class method
def self.population
@@population
end
private
def valid_email(email)
if email != '' && !@@emails.include?(email)
valid = true
else
valid = false
end
valid
end
end
class Player < Person
attr_accessor :score
def initialize(name, email)
@score = 0
super(name, email)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment