Skip to content

Instantly share code, notes, and snippets.

@mkoby
Created March 30, 2012 20:34
Show Gist options
  • Save mkoby/2254787 to your computer and use it in GitHub Desktop.
Save mkoby/2254787 to your computer and use it in GitHub Desktop.
Intro to Ruby - 08 - Classes: Part 02
## 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