Skip to content

Instantly share code, notes, and snippets.

@koriroys
Last active September 12, 2015 10:20
Show Gist options
  • Save koriroys/08d9707c2f9ce652f742 to your computer and use it in GitHub Desktop.
Save koriroys/08d9707c2f9ce652f742 to your computer and use it in GitHub Desktop.
For Ruby Wednesday Sep 16, 2015
# Fork this gist, then fill in the code with your answers.
# you teach an advanced mathematics class at the local university.
# Your class is very difficult, so all but three students have quit.
# The three students who remain are:
# jim, 22 years old
# caroline, age 21 years old
# jasmine, the 17 year old young genius
# to keep track of your students, you create a Student class:
class Student
attr_accessor :name, :age
def initialize(name:, age:)
@name = name
@age = age
end
end
# then you create a list of all your students
students = [
Student.new(name: 'jim', age: 22),
### TODO: fill in the blanks with the other two students
Student.new(name: 'caroline', age: 21),
Student.new(name: 'jasmine', age: 17)
]
# you want the list out all the names of your students with an easy command,
# so you create the names method
class Display
def initialize(students)
@students = students
end
def names
names = []
@students.each do |student|
names << student.name.capitalize
end
puts names.join(", ")
end
def ages
# you also want to list the ages of your students. fill in this method
# so that it prints out all the ages of your students.
# EXPECTED OUTPUT:
# 22, 21, 17
end
def average_age
# fill in this method with the code to print out the average age of the class
# hint: try to use a different method than 'each' from the Enumerable class
# hint: (or maybe a combination of methods)
# EXPECTED OUTPUT:
# average age of class: 20 years old
end
def student_info(student_name)
# hint: you'll need to find the right student in the list of students,
# then fetch that student's age as well.
# EXPECTED OUTPUT, given a student_name of "jim":
# Jim is 22 years old
end
end
display = Display.new(students)
display.names
display.ages
display.average_age
display.student_info("jim")
display.student_info("jasmine")
## final output should be:
# Jim, Caroline, Jasmine
# 22, 21, 17
# average age of class: 20 years old
# Jim is 22 years old
# Jasmine is 17 years old
# when you finish, add a screenshot of the terminal output to your forked gist
@stefatkins
Copy link

Done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment