-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectoryex2.rb
51 lines (47 loc) · 1.2 KB
/
directoryex2.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
47
48
49
50
51
#let's put all students into an array
class String
def initial
self[0,1]
end
end
students = []
def input_students
puts "Please enter the names of the students"
puts "To finish, just hit return twice"
# create an empty array
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
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
def print_header
puts "The students of Villains Academy"
puts "-------------"
end
def print_letter(students)
puts "What letter would you like me to search?"
letter = gets.chomp
letter = letter.upcase
students.each_with_index do |student, index|
if letter == student[:name].initial
puts "#{index}. #{student[:name]} (#{student[:cohort]} cohort)"
end
end
end
def print_footer(students)
puts "Overall, we have #{students.count} great students"
end
#nothing happens until we call the methods
students = input_students
print_header
print_letter(students)
print_footer(students)