Created
March 6, 2015 22:51
-
-
Save mayfer/6c03d91bca62f780532b to your computer and use it in GitHub Desktop.
Introduction to Objects and Classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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