Skip to content

Instantly share code, notes, and snippets.

@sgrshah
Created July 22, 2013 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgrshah/e7c845b5f0b3fedb59ba to your computer and use it in GitHub Desktop.
Save sgrshah/e7c845b5f0b3fedb59ba to your computer and use it in GitHub Desktop.
SagarShah_assessment.rb
SagarShah_assessment.rb
# 1. Arrays
array = ["Blake", "Ashley", "Jeff"]
array << "Avi"
puts array
array[1]
array.index("Jeff")
# 2. Hashes
instructor = {:name=>"Ashley", age: 27}
instructor["location"] = "NYC"
puts instructor
instructor[:name]
instructor.key(27)
# 3. Nested Structures
school = { :name => "Happy Funtime School",
:location => "NYC",
:instructors => [ {:name=>"Blake",
:subject=>"being awesome" },
{:name=>"Ashley",
:subject=>"being better than blake"},
{:name=>"Jeff",
:subject=>"karaoke"}],
:students => [ {:name => "Marissa",
:grade => "B"},
{:name=>"Billy",
:grade => "F"},
{:name => "Frank",
:grade => "A"},
{:name => "Sophie",
:grade => "C"}
]
}
school[:founded_in] = 2013
school[:students] << "Sagar"
school[:students].delete_at(1)
school[:students].each do |student|
student[:semester] = "Summer"
end
school[:instructors][1][:subject] = "being almost better than Blake"
school[:students][2][:grade] = "F"
school[:students][0][:name]
school[:instructors][2][:subject]
puts school.values
# 4. Methods
# not entirely sure if this is done correctly.
def grade(name, school)
school[:students].select do |student|
if student[:name] == name
student[:grade]
end
end
end
# i. not sure what the refactoring part of this question is asking
def update_subject(instructor_name,subject,school)
school[:instructors].select do |instructor|
if instructor[:name] == instructor_name
instructor[:subject] = subject
end
end
end
update_subject("Blake", "being terrible", school)
def add_student(name, grade, school)
school[:students] << {:name => name, :grade => grade}
end
add_student("Sagar", "A", school)
def add_new_key(key, value, school)
school[key] = value
end
add_new_key("Ranking", 1, school)
# 5. Object Orientation
class School
SCHOOLS =[]
attr_accessor :name, :location, :instructors, :students
attr_reader :ranking
def initialize(name, location, ranking, students, instructors)
#assuming that all parameters get passed in as previously
@name = name
@location = location
@ranking = ranking
@students = students
@instructors = instructors
add_school
end
def add_school
SCHOOLS << self
end
def self.reset
SCHOOLS = []
end
def ranking=(value)
@ranking = value
end
def add_student(name, grade, semester)
@students << {:name => name, :grade => grade, :semester => semester}
end
def remove_student(name)
@students.each_with_index do |student, i|
@students.delete_at(i) if student[:name] == name
end
end
end
# 6. Classes
class Student
attr_accessor :name, :semester, :grade
STUDENTS =[]
def self.find_by_name(name_parameter)
STUDENTS.select do |student|
student if student.name == name_parameter
end
end
end
# Refactoring for student object.
class School
SCHOOLS =[]
attr_accessor :name, :location, :instructors, :students
attr_reader :ranking
def initialize(name, location, ranking, students, instructors)
@name = name
@location = location
@ranking = ranking
@students = students
@instructors = instructors
add_school
end
def add_school
SCHOOLS << self
end
def self.reset
SCHOOLS = []
end
def ranking=(value)
@ranking = value
end
#refactored
def add_student(student)
@students << student
end
#refactored
def remove_student(name_parameter)
@students.each_with_index do |student, i|
@students.delete_at(i) if student.name == name_parameter
end
end
end
# 7. Self
# a. "hello" "Student"
# b. "Student"
# c. the instance of the student that was created as a result of the self.new method call.
# d. the instance of the student that was created as a result of the Student.new method call.
# e. "goodbye"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment