Created
March 30, 2012 20:34
-
-
Save mkoby/2254787 to your computer and use it in GitHub Desktop.
Intro to Ruby - 08 - Classes: Part 02
This file contains 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
## Here is a basic class, with a basic | |
## initialize method that takes a name | |
## argument | |
class Employee | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
end | |
## Now we create a new Employee object, with | |
## the name already initialized | |
e = Employee.new(“Michael Koby”) | |
e.name # => "Michael Koby" | |
## Now we'll edit the Employee class to add | |
## and read only age property and change the | |
## initialize method to also take an age argument | |
class Employee | |
attr_reader :age | |
def initialize(name, age) | |
@name = name | |
@age = age | |
end | |
end | |
## Now we create a new Employee object, with the | |
## name and age initialized | |
e = Employee.new(“Michael Koby”, 32) | |
e.name # => "Michael Koby" | |
e.age # => 32 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment