Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created February 6, 2014 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayfer/8836489 to your computer and use it in GitHub Desktop.
Save mayfer/8836489 to your computer and use it in GitHub Desktop.
Example ruby exercise
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