Tod <todbramble <at> gmail.com> writes:

> An example might be a course registration system. In this system you
> have student objects and a registration object.  Your student object
> can call the method student.register_for_course method. Now, before the
> studen can register for the course we need to make sure the student has
> met all the prerequisites.  So ideally the student object would pass
> the request to the registration object along with the name of the
> course, the students id, etc. The registration object would call the
> registration.prerequisite_met? method.  Then it would return the
> response back to the student object which could then procede with
> either registering for the class or indicate that registration would
> not be allowed.

Well, one way to do it would be as follows. You could argue that there are
better ways to fail Registration generation then raising exceptions, but it's
just an idea..

====================================
class Course
  attr_accessor :name
  attr_accessor :number
end

class Student
  attr_accessor :name
end

class Registration
  def initialize(student, course)
    if student.name == "Bob"
      raise ArgumentError, "Student doesn't fulfill prerequisites for the course"
    end
  end
end

s = Student.new
s.name = Bob
c = Course.new
c.name = "Advanced Hydronomics"
c.number = 1234
r = Registration.new(s,c)
====================================

Gareth