Last active
December 23, 2015 11:18
-
-
Save andersr/6626928 to your computer and use it in GitHub Desktop.
FS 003 Ruby Assessment
This file contains hidden or 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
# Ruby Assessment: https://gist.github.com/aviflombaum/ae78e0559cf51a58aad7 | |
# Name: Anders Ramsay | |
### 1. Arrays ### | |
array = ["Blake","Ashley","Jeff"] | |
# a. Add an element to an (existing) array. | |
array << "Bill" | |
# b. Write a statement to print out all the elements of the array. | |
array.each { |array_element| puts array_element } | |
# c. Return the value at index 1. | |
array[1] | |
# d. Return the index for the value "Jeff". | |
array.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 { |key,value| puts "#{key}: #{value}" } | |
# 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. | |
new_student = {:name => "Bob", :grade => "A+"} | |
school[:students].push(new_student) | |
# c. Remove "Billy" from the students' array. | |
school[:students].delete_if {|student| student[:name] == "Billy"} | |
# d. Add a key to every student in the students array called "semester" and assign it the value "Summer". | |
new_semester = {:semester => "Summer"} | |
school[:students].each {|student| student.merge!(new_semester)} | |
# e. Change Ashley's subject to "being almost better than Blake" | |
instructor = school[:instructors].find {|instructor| instructor[:name] == "Ashley"} | |
instructor[:subject] = "being almost better than Blake" | |
# f. Change Frank's grade from "A" to "F". | |
student = school[:students].find {|student| student[:name] == "Frank"} | |
student[:grade] = "F" | |
# g. Return the name of the student with a "B". | |
student = school[:students].find {|student| student[:grade] == "B"} | |
student[:name] | |
# h. Return the subject of the instructor "Jeff". | |
instructor = school[:instructors].find {|instructor| instructor[:name] == "Jeff"} | |
instructor[:subject] | |
# i. Write a statement to print out all the values in the school. ***FLAG | |
school.each do |key,value| | |
if value.kind_of?(Array) | |
value.each do |array_element| | |
array_element.each do |key,value| | |
puts value | |
end | |
end | |
else | |
puts value | |
end | |
end | |
### 4. Methods | |
# Note: You will need to pass the school variable to each of these methods | |
# to include it in scope. | |
# a. | |
# i. Create a method to return the grade of a student, | |
# given that student's name. | |
def student_grade(school,student_name) | |
student = school[:students].find {|student| student[:name] == student_name} | |
student_grade = student[:grade] | |
end | |
# ii. Then use it to refactor your work in 3.i. | |
# b. i.Create a method to update a instructor's subject | |
# given the instructor and the new subject. | |
def instructor_subject(school,instructor_name,instructor_subject) | |
instructor = school[:instructors].find {|instructor| instructor[:name] == instructor_name} | |
instructor[:subject] = instructor_subject | |
end | |
# ii. Then use it to update Blake's subject to "being terrible". | |
instructor_subject(school,"Blake", "being terrible") | |
# c. i. Create a method to add a new student to the schools student array. | |
# ii. Then use it to add yourself to the school students array. | |
def add_student(school,new_student_name) | |
new_student = {:name => new_student_name} | |
school[:students].push(new_student) | |
end | |
add_student(school,"Anders") | |
# 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_to_school(school,new_key,new_value) | |
new_key_value_pair = {new_key => new_value} | |
school.merge!(new_key_value_pair) | |
puts school[new_key] | |
end | |
# ii. Then use it to add a "Ranking" key with the value 1. | |
add_key_to_school(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 | |
def initialize() | |
end | |
end | |
# 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. | |
class School | |
def initialize(name, location, ranking, students, instructors) | |
@name = name | |
@ranking = ranking | |
@location = location | |
@instructors = instructors[] | |
@students = stuents[] | |
end | |
end | |
# 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. | |
class School | |
attr_accessible :name, :location, :instructors, :students | |
attr_reader :ranking | |
def initialize(name, location, ranking, students, instructors) | |
@name = name | |
@ranking = ranking | |
@location = location | |
@instructors = instructors[] | |
@students = stuents[] | |
end | |
end | |
# d. Create a method to set ranking, given a ranking value. | |
def set_rank(school,student,rank) | |
student = school[:students].find {|student| student[:name] == student_name} | |
student[:rank] = rank | |
end | |
# e. Create a method to add a student to the school, given a name, a grade, and a semester. | |
def add_student(school,name,grade,semester) | |
new_student = {:name => name, :grade => grade, :semester => semester } | |
school[:students].push(new_student) | |
end | |
# f. Create a method to remove a student from the school, given a name. | |
def remove_student(school,name) | |
student = school[:students].find {|student| student[:name] == name} | |
school[:students].delete(student) | |
end | |
# g. Create an array constant SCHOOLS that stores all instances of your School class. | |
SCHOOLS = [@name, @ranking, @location, @instructors, @students] | |
# h. Create a class method reset that will empty the SCHOOLS constant. | |
def reset_school | |
SCHOOLS.clear | |
end | |
### 6. Classes | |
# a. Create a Student class. | |
class Student | |
end | |
# b. Refactor your School instance methods to treat Students as an array of objects instead of an array of hashes. | |
# c. Create a method in the School class that finds a student by name and returns the correct Student object. | |
### 7. Self | |
# For this section, please use the letters and answer each individually. | |
# Note: in cases where self is an instance of an object just note that | |
# as the object id printed to the screen is going to be different everytime | |
# a.What should this Class print to the screen when defined/loaded? | |
class Student | |
def self.say_hello | |
puts "hello" | |
end | |
say_hello | |
puts self | |
end | |
#Output: | |
hello | |
Student | |
# b. What should this Class print to the screen when defined/loaded? | |
class Student | |
def self.say_hello | |
puts self | |
end | |
say_hello | |
end | |
#Output: | |
Student | |
# c. What should this Class print to the screen when defined/loaded? | |
class Student | |
def initialize | |
puts self | |
end | |
new | |
end | |
#Output: | |
#<Student:0x007ffd289bef10> | |
# d. What should this code print to the screen when run? | |
class Student | |
def say_hello | |
puts self | |
end | |
end | |
Student.new.say_hello | |
#Output: | |
#<Student:0x007fdbf198ae98> | |
# e. What should this code print to the screen when run? | |
class Student | |
def say_hello | |
puts say_goodbye | |
end | |
def say_goodbye | |
"goodbye" | |
end | |
end | |
Student.new.say_hello | |
#Output: | |
goodbye |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment