-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory.rb
46 lines (39 loc) · 1.26 KB
/
directory.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Create method which asks user to input student names
def input_students
puts "Please enter the names of the students"
puts "To finish, just hit return twice"
# Create an empty array to store our future students
students = []
# Get the first name
name = gets.chomp
# While the name is not empty, repeat this code
while !name.empty? do
# Add the student hash to the array. << is the shovel operator, which puts things into an array
students << {name: name, cohort: :november}
puts "Now we have #{students.count} students"
# Get another name from the user
name = gets.chomp
end
# Return the array of students
students
end
# Create a method for the header
def print_header
puts "The students of Villain Academy"
puts "-----------------"
end
# Create method to iterate over array of students. Student info now in a hash, so use key symbols within array [].
def print(students)
students.each do |student|
puts "#{student[:name]} (#{student[:cohort]} cohort)"
end
end
# Create method to print footer
def print_footer(names)
puts "Overall, we have #{names.count} great students"
end
# Run methods, its print(students) as it will be printing the students array
students = input_students
print_header
print(students)
print_footer(students)