Created
February 6, 2014 00:53
-
-
Save mayfer/8836489 to your computer and use it in GitHub Desktop.
Example ruby exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Exam | |
attr_accessor :passes | |
attr_accessor :fails | |
def initialize(students) | |
@students = students | |
@passes = [] | |
@fails = [] | |
end | |
def complete | |
@students.each do |student| | |
student.take_exam(self) | |
if student.score < 50 | |
@fails << student | |
else | |
@passes << student | |
end | |
end | |
end | |
end | |
class Student | |
attr_reader :score | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
def take_exam(exam) | |
@score = rand() * 100 | |
end | |
def to_s | |
"#{@name}" | |
end | |
end | |
students = [ | |
Student.new("Whoever"), | |
Student.new("Nobody"), | |
Student.new("grant"), | |
Student.new("murat"), | |
] | |
exam = Exam.new(students) | |
exam.complete() | |
puts "Passes: #{exam.passes.join(', ')}" | |
puts "Fails: #{exam.fails.join(', ')}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment