forked from RubyoffRails/seniors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent.rb
More file actions
44 lines (35 loc) · 766 Bytes
/
Copy pathstudent.rb
File metadata and controls
44 lines (35 loc) · 766 Bytes
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
class Student
attr_reader :first_name, :last_name, :grade
def initialize(first_name, last_name, grade)
@first_name = first_name
@last_name = last_name
@grade = grade
end
def senior?
grade == 12
end
def junior?
grade < 12
end
def to_s
"#{last_name}, #{first_name}"
end
end
def seniors(students)
students.select { |student| student.senior? }
end
def juniors(students)
students.select { |student| student.junior? }
end
fred = Student.new("Fred", "James", 12)
sarah = Student.new("Sarah", "Smith", 12)
jack = Student.new("Jack", "Gong", 11)
all_students = [fred, sarah, jack]
puts "Seniors"
seniors(all_students).each do |student|
puts "* #{student}"
end
puts "\nJuniors"
juniors(all_students).each do |student|
puts "* #{student}"
end