Created
July 22, 2013 16:34
-
-
Save ajwaxman/6055337 to your computer and use it in GitHub Desktop.
ruby assessment
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
# AdamWaxman_assessment.rb | |
############# | |
# 1. Arrays # | |
############# | |
array = ["Blake", "Ashley", "Jeff"] | |
# a. Add a element to an array | |
array.push("Adam") | |
# b. Write a statement to print out all the elements of the array. | |
array.each do |element| | |
puts element | |
end | |
# c. Return the value at index 1. | |
array[1] | |
# d. Return the index for the value "Jeff". | |
array.find_index("Jeff") | |
############# | |
# 2. Hashes # | |
############# | |
instructor = {:name=>"Ashley", :age=>27} | |
# a. Add a new key for location and give it the value "NYC". | |
instructor[:location] = "NYC" | |
# b. Write a statement to print out all the key/value pairs in the hash | |
instructor.each do |key, value| | |
puts "Key:#{key}, value: #{value}" | |
end | |
# c. Return the name value from the hash. | |
instructor[:name] | |
# d. Return the key name for the value 27. | |
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"} | |
] | |
} | |
# a. Add a key to the school hash called "founded_in" and set it to the value 2013. | |
school[:founded_in] = 2013 | |
# b. Add a student to the school's students' array. | |
school[:students].push({:name => "Bob", :grade => "A"}) | |
# c. Remove "Billy" from the students' array. | |
school[:students].delete_at(1) | |
# d. Add a key to every student in the students array called "semester" and assign it the value "Summer". | |
school[:students].each do |student| | |
student[:semester] = "Summer" | |
end | |
# e. Change Ashley's subject to "being almost better than Blake" | |
school[:instructors][1][:subject] = "being almost bettter than Blake" | |
# f. Change Frank's grade from "A" to "F". | |
school[:students][1][:grade] = "B" # Assumes object was already manipulated and that Billy was removed from the student array | |
# g. Return the name of the student with a "B". | |
school[:students].each do |student| | |
if student[:grade] == "B" | |
puts student[:name] | |
return student[:name] | |
end | |
end | |
########### Come Back to Above Problem ############## | |
# h. Return the subject of the instructor "Jeff". | |
school[:instructors].last[:subject] | |
# i. Write a statement to print out all the values in the school. ***FLAG | |
school.values | |
############## | |
# 4. Methods # | |
############## | |
# a. | |
# i. Create a method to return the grade of a student, given that student's name. | |
def student_grade(school, name) | |
grade = nil | |
school[:students].each do |student| | |
if student[:name] == name | |
grade = student[:grade] | |
end | |
end | |
return grade | |
end | |
# ii. Then use it to refactor your work in 3.i. | |
######## Confused what this question is asking | |
# b. | |
# i.Create a method to udpate a instructor's subject given the instructor and the new subject. | |
def update_subject(school, instructor_name, new_subject) | |
school[:instructors].each do |instructor| | |
if instructor[:name] == instructor_name | |
instructor[:subject] = new_subject | |
end | |
end | |
end | |
# ii. Then use it to update Blake's subject to "being terrible". | |
update_subject(school, "Blake", "being terrible") | |
# c. | |
# i. Create a method to add a new student to the schools student array. | |
def add_student(school, student) | |
school[:students].push(student) | |
end | |
# ii. Then use it to add yourself to the school students array. | |
adam = {:name => "Adam", :grade => "TBD"} | |
add_student(school, adam) | |
# d. | |
# i. Create a method that adds a new key at the top level of the school hash, given a key and a value. | |
def add_key(school, key, value) | |
school[key] = value | |
end | |
# ii. Then use it to add a "Ranking" key with the value 1. | |
add_key(school, "Ranking", 1) | |
######################### | |
# 5. Object Orientation # | |
######################### | |
# a. Create a bare bones class definition for a School class. | |
class School | |
end | |
# b. Define an initialize method for the School class. | |
class School | |
attr_accessor :name, :location, :students, :instructors | |
attr_reader :ranking | |
def initialize(name, location, ranking, students, instructors) | |
self.name = name | |
self.location = location | |
self.ranking = ranking | |
self.students = students | |
self.instructors = instructors | |
end | |
def ranking=(value) | |
self.ranking = value | |
end | |
def add_student(school, name, grade, semester) | |
end | |
end | |
name = "Hawken" | |
location = "Cleveland" | |
ranking = 1 | |
students = [{:name => "Adam", :grade => "TBD"}, {:name => "Michael", :grade => "TBD"}] | |
instructors = [{:name => "Avi", :subject => "Ruby"}, {:name => "Bob", :subject => "Physics"}] | |
# i. Give your School class the instance variables: name, location, ranking, students, instructors. | |
# **NOTE: These variables should be of the same type as they are in the hash above. | |
# ii. Rewrite your initialize method definition to take a parameter for each instance variable. | |
# iii. Initialize each instance variable with the value of the corresponding parameter. | |
# c. Create an attr_accessor for name, location, instructors, and students. Create an attr_reader for ranking. | |
# d. Create a method to set ranking, given a ranking value. | |
# e. Create a method to add a student to the school, given a name, a grade, and a semester. | |
# f. Create a method to remove a student from the school, given a name. | |
# g. Create an array constant SCHOOLS that stores all instances of your School class. | |
# h. Create a class method `reset` that will empty the SCHOOLS constant. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment