-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaythree.rb
118 lines (92 loc) · 2.96 KB
/
daythree.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require 'date'
def print_header
puts "The students of my cohort at Makers Academy\n".center(100)
puts "-------------\n".center(100)
end
def print_names(students)
# students.each.with_index(1) do |student, index|
i = 0
while i < students.length do
puts "#{students[i][:name]} (#{students[i][:cohort]} cohort) \n".center(100)
i += 1 #if (student[:name].downcase.start_with?("a") && student[:name].length < 4)
end
end
def print_footer(names)
print center("Overall, we have #{names.length} great students \n")
end
def ask_for(value)
puts center("#{value}? =>")
gets.chomp
end
def sort_by_cohort!(students)
students.sort_by!{|student| student[:date]}
end
def pluralize(value)
return "student" if value == 1
"students"
end
def input_students
puts "Please enter the names of the students".center(100)
print "To finish, just hit return twice\n".center(100)
# create an empty array
students = []
# get the first name
name = ask_for('name')
cohort = ask_for('cohort')
while !(Date.parse(cohort) rescue nil)
puts "You spelt the cohort wrong"
cohort = ask_for('cohort')
end
while !name.empty? do
students << {:name => name, :cohort => cohort, :date => Date.parse(cohort)}
print "Now we have #{students.length} #{pluralize(students.length)}\n".center(100)
name = ask_for('name')
cohort = ask_for('cohort')
while !(Date.parse(cohort) rescue nil)
puts "You spelt the cohort wrong"
cohort = ask_for('cohort')
end
end
students
end
# while the name is not empty, repeat this code
# add the student hash to the array
# get another name from the user
# return the array of students
def center(string)
string.center(100)
end
# if cohort.empty?
# cohort = "June"
# end
students = input_students
print_header
sort_by_cohort!(students)
print_names(students)
print_footer(students)
# def input_students
# puts "Please enter the names of the students".center(100)
# print "To finish, just hit return twice\n".center(100)
# # create an empty array
# students = []
# # get the first name
# name = ask_for('name')
# cohort = ask_for('cohort')
# while !(Date.parse(cohort) rescue nil)
# puts "You spelt the cohort wrong"
# cohort = ask_for('cohort')
# end
# students << {:name => name, :cohort => cohort, :date => Date.parse(cohort)}
# # while the name is not empty, repeat this code
# while !name.empty? do
# # add the student hash to the array
# print "Now we have #{students.length} students\n".center(100)
# # get another name from the user
# name = ask_for('name')
# cohort = ask_for('cohort')
# students << {:name => name, :cohort => cohort, :date => Date.parse(cohort)}
# while !(Date.parse(cohort) rescue nil)
# puts "You spelt the cohort wrong"
# cohort = ask_for('cohort')
# end
# end