Skip to content

Instantly share code, notes, and snippets.

@woduda
Last active January 23, 2016 13:07
Show Gist options
  • Save woduda/1ffc223b3b6d774ef47b to your computer and use it in GitHub Desktop.
Save woduda/1ffc223b3b6d774ef47b to your computer and use it in GitHub Desktop.
Student with best grades (max 10 Students if created with factory)
class Student
MAX = 10
@@students = []
attr_accessor :name, :surname, :specialization, :grades
def self.factory(hash={})
return nil if @@students.length >= MAX
student = Student.new(hash)
@@students << student
student
end
def initialize(hash={})
@name = hash[:name].capitalize if hash.has_key? :name
@surname = hash[:surname].capitalize if hash.has_key? :surname
@specialization = hash[:specialization]
@grades = hash[:grades]
end
def to_s
"#{@name} #{@surname}, #{@specialization}, #{@grades}"
end
def self.best_student
@@students.max_by { |s| s.grades }
end
end
p Student.factory(name: "wojtek", surname: "duda", grades: 5)
p Student.factory(name: "ala", surname: "kowalska", grades: 6)
p Student.factory(name: "Monika", surname: "Nowak", grades: 2)
8.times do
p Student.factory(name: "wojtek", surname: "duda", grades: 5)
end
p Student.best_student
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment