Homework 4 - Ruby
In this homework, you will exercise all that you've learned about Ruby,
and you will learn a new object-oriented concept: delegation.
Code Base here.
What to turn in: one zip file containing: your working code and an
extra file for question 1.
The zip file that you are receiving contains the basic Ruby program
that you are going to complete and extend. It consists of 4 ruby files:
test.rb -- the "main" program that tests the classes and objects of the
application
people.rb -- the Person hierarchy, i.e. people's official positions in
a University
roles.rb -- the Role hierarchy, i.e. roles that people play
policies.rb -- the policies associating people with roles
The application consists of people objects that can play several roles
dynamically. People come in different types: Faculty, Student (Graduate
and Undergraduate), and Staff. The roles are: Instructor,
Administrator, and Researcher.
Role-playing is achieved through delegation.
In a nutshell, delegation is the ability of an object to delegate to
another object the servicing of certain method calls. Here is an
example:
class A
end
class B
def m
end
end
When we associate A-objects with B-objects through delegation,
A-objects acquire the ability to respond to message m, even though that
method is not defined in A. So A.new.m is a valid method call.
Furthermore, the association is dynamic, i.e. we can switch the
delegator B-object as the program runs.
The delegation association between A-objects and B-objects can be done
in different ways in Ruby. To learn more about Ruby's delegation
mechanism, please Google it. In the example, it is done by having the
class Person extend a special Ruby class SimpleDelegator (look inside
people.rb), and then calling the method __setobject___ defined in
SimpleDelegator (look inside roles.rb). You need to understand
delegation before you can complete the assignment, so make sure you
ready through the basic documentation.
Now, for the concrete assignment:
1 -- Draw a boxes-and-arrows diagram showing the (static) relation
between the classes and modules defined in the files people.rb and
roles.rb. This doesn't need to be UML, but it needs to be
understandable. For classes, tag the boxes with "class"; for modules,
tag the boxes with "module". Inside the boxes, write down the methods
of those classes and modules. The arrows should have the relation name,
for example, "includes", "extends", etc. The diagram can be drawn in
any figure editor, such as powerpoint and MS Word. If you decide to use
something other than powerpoint or MS Word, please provide the result
in PDF.
2 -- Complete the code, so that the program works. Look for places in
the code saying TODO -- they are only in test.rb and roles.rb, ignore
policies.rb for now. You are not supposed to modify the code that's
already there; you should only complete the TODOs. The output of the
completed program should be:
Creating objects...
Presenting objects:
Hi! I'm Michael. I'm
a Undergraduate.
Hi! I'm Annette. I'm
a Staff.
Hi! I'm Susan. I'm a
Faculty.
Hi! I'm Sushil. I'm
a Graduate.
Hi! I'm Debra. I'm a
Faculty.
Hi! I'm Marty. I'm a
Staff.
Hi! I'm Mitch. I'm a
Undergraduate.
Hi! I'm Tosin. I'm a
Graduate.
What objects do for
work:
Michael: Study Study
Study.
Annette: Work Work
Work.
Susan: Blah Blah
Blah.
Sushil: Study Study
Study.
Debra: Blah Blah
Blah.
Marty: Work Work
Work.
Mitch: Study Study
Study.
Tosin: Study Study
Study.
Some objects also
have fun:
Michael: Party Party
Party.
Sushil: Read papers.
Mitch: Party Party
Party.
Tosin: Read papers.
Associating roles to
objects...
Susan now playing
Instructor.
Debra now playing
Administrator.
Tosin now playing
Instructor.
Sushil now playing
Researcher.
Annette now playing
Administrator.
Objects playing
roles:
Annette does not
teach.
Annette: Manage
Manage Manage.
Annette does not
research.
Susan: Teach Teach
Teach.
Susan does not
manage.
Susan does not
research.
Sushil does not
teach.
Sushil does not
manage.
Sushil: Research
Research Research.
Debra does not teach.
Debra: Manage Manage
Manage.
Debra does not
research.
Tosin: Teach Teach
Teach.
Tosin does not
manage.
Tosin does not
research.
Removing roles...
Objects playing
roles:
3 -- Look in test.rb, method objects_play_roles. There is a comment
just above that method. Please use that comment to explain how is it
that Person objects can respond to messages teach, manage and research,
even though those methods are not defined anywhere in the Person
hierarchy. Shouldn't this give an error? (give the answer directly in
the comment in the code, not in any separate text file)
4 -- Look in roles.rb, class Role. There is a comment just above method
"method_missing". Please add that comment, i.e. explain why that method
is there, and how that method is supposed to work. Under what
circunstances are methods missing? (give the answer directly
in
the comment in the code, not in any separate text file)
5 -- As is now, any person can play any role. That's not how
organizations work, there's usually a policy ruling who can play what.
Implement the policy (in policies.rb), and modify the method
play_role in module RolePlay, so that the policy is enforced.
Then
test it in the main function (test.rb) by uncommenting the last few
lines in that funtion. The output should now also include the following
lines at the end (after the same output as above):
Associating roles to
objects...
Susan now playing
Researcher.
Debra now playing
Instructor.
Tosin cannot play
Administrator.
Mitch now playing
Researcher.
Michael cannot play
Instructor.
Annette cannot play
Researcher.
Objects playing
roles:
Susan does not teach.
Susan does not
manage.
Susan: Research
Research Research.
Debra: Teach Teach
Teach.
Debra does not
manage.
Debra does not
research.
Mitch does not teach.
Mitch does not
manage.
Mitch: Research
Research Research.