Skip to content

Instantly share code, notes, and snippets.

@ashleygwilliams
Last active December 17, 2015 22:29
Show Gist options
  • Save ashleygwilliams/5682375 to your computer and use it in GitHub Desktop.
Save ashleygwilliams/5682375 to your computer and use it in GitHub Desktop.
practicing instance variables/methods and class variable/methods
#CLASS/INSTANCE METHODS AND VARIABLES PRACTICE:
#
#1. create a class called Project
class Project
#3. make the instance variables accessible by dot notation EXCEPT for date updated
attr_accessor :title, :description, :role, :date_created
#1. create a class variable called count
@@count = 0
#2. create a class method to return @@count
def self.count
@@count
end
#2. create a class constant that holds an array with thenumbers 1-10
MYARRAY = [1 ,12, 10]
#1. create a class method called "sayClassName" that prints name of the class
def self.sayClassName
self
end
#1. create a instance method called "sayClassName" that prints name of the class
def sayClassName
self.class
end
#2. give it the instance variables title, description, role, date created and date updated
def initialize(title, description, role, date_created, date_updated)
@title = title
@description = description
@role = role
@date_created = date_created
@date_updated = date_updated
#count everytime initialize is run
@@count = @@count + 1
end
#4. make a instance method called getDateUpdated to return date_updated
def getDateUpdated
return @date_updated
end
#5. make an instance method called changeDateUpdated to change the value of date_updated. this method should print "successfully updated <Project title>."
def changeDateUpdated(newValue)
@date_updated = newValue
puts "Successfully updated" + @title
end
end
Test the interface of your class:
(place this code in the same ruby file below the class definiton)
* make a new project #e.g. Project.new()
* access instance variable values
* using dot notation
* using instance methods
* change instance variable values
* using dot notation
* using instance methods #e.g. myProject.changeDateUpdated("05 May 1986")
* access the count of objects created
* using ::
* using a class method
THEN... when you are sure it works, write in English how the program goes line by line to make it work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment