During this lab session:
This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.
IMPORTANT NOTE: The lab activities can be developed individually or in pairs. The lab report must be developed individually.
Create a folder called metaprogramming. Inside this folder, create two files called:
attr_init.rb and
test_attr_init.rb.
All Ruby source files must start with a comment containing the lab's title, date, and the authors' personal information. For example:
# Lab 9: Meta-programming Pattern # Date: 28-Mar-2012 # Authors: # 456654 Thursday Rubinstein # 1160611 Anthony Stark
In the attr_init.rb source file, open the Object class and define a new class method called attr_init. This method should work as a class annotation that defines the initialize method in the enclosing class, and also calls the attr_accessor as shown as follows.
For example, given the class definition:
class C attr_init :w, :x, :y, :z end
It should be transformed into something equivalent to this:
class C
def initialize(w, x, y, z)
@w = w
@x = x
@y = y
@z = z
end
attr_accessor :w, :x, :y, :z
end
Use the meta-programming pattern, as explained in page 309 of [OLSEN], to do this.
The following unit tests verify the correct definition of the attr_init class annotation. Place the test class in the test_attr_init.rb source file.
require 'test/unit'
require 'attr_init'
class AttrInitTest < Test::Unit::TestCase
class Alpha
attr_init :beta, :gamma, :delta
end
class BiologicalClassification
attr_init :kingdom, :phylum, :klass, :order, :family, :genus, :species
end
def test_alpha
alpha = Alpha.new('one', 'two', 'three')
assert_equal 3, alpha.method(:initialize).arity
assert_respond_to alpha, :beta
assert_respond_to alpha, :beta=
assert_respond_to alpha, :gamma
assert_respond_to alpha, :gamma=
assert_respond_to alpha, :delta
assert_respond_to alpha, :delta=
assert_equal 'one', alpha.beta
assert_equal 'two', alpha.gamma
assert_equal 'three', alpha.delta
assert_equal 1, (alpha.beta = 1)
assert_equal 2, (alpha.gamma = 2)
assert_equal 3, (alpha.delta = 3)
assert_equal 1, alpha.beta
assert_equal 2, alpha.gamma
assert_equal 3, alpha.delta
end
def test_biological_classification
platapus = BiologicalClassification.new(
'Animalia',
'Chordata',
'Mammalia',
'Monotremata',
'Ornithorhynchidae',
'Ornithorhynchus',
'Ornithorhynchus Anatinus')
assert_equal 7, platapus.method(:initialize).arity
assert_respond_to platapus, :kingdom
assert_respond_to platapus, :phylum
assert_respond_to platapus, :klass
assert_respond_to platapus, :order
assert_respond_to platapus, :family
assert_respond_to platapus, :genus
assert_respond_to platapus, :species
assert_respond_to platapus, :kingdom=
assert_respond_to platapus, :phylum=
assert_respond_to platapus, :klass=
assert_respond_to platapus, :order=
assert_respond_to platapus, :family=
assert_respond_to platapus, :genus=
assert_respond_to platapus, :species=
assert_equal 'Animalia', platapus.kingdom
assert_equal 'Chordata', platapus.phylum
assert_equal 'Mammalia', platapus.klass
assert_equal 'Monotremata', platapus.order
assert_equal 'Ornithorhynchidae', platapus.family
assert_equal 'Ornithorhynchus', platapus.genus
assert_equal 'Ornithorhynchus Anatinus', platapus.species
assert_equal 'Amoebozoa', (platapus.kingdom = 'Amoebozoa')
assert_equal 'Tubulinea', (platapus.phylum = 'Tubulinea')
assert_equal 'Tubulinida', (platapus.order = 'Tubulinida')
assert_equal 'Amoebidae', (platapus.family = 'Amoebidae')
assert_equal 'Amoeba', (platapus.genus = 'Amoeba')
assert_equal 'Amoeba Proteus', (platapus.species = 'Amoeba Proteus')
end
def test_errors
assert_raise(ArgumentError) do
alpha = Alpha.new()
end
assert_raise(ArgumentError) do
alpha = Alpha.new(1, 2, 3, 4)
end
assert_raise(ArgumentError) do
alpha = BiologicalClassification.new()
end
assert_raise(ArgumentError) do
alpha = BiologicalClassification.new(
'Plantae',
'Magnoliophyta',
'Magnoliopsida',
'Rosales',
'Rosaceae')
end
end
end
To hand in your individual lab work, follow these instructions.
lab9_report_A0MMMMMMM.txt, where A0MMMMMMM is your student ID. From your AsciiDoc source, generate the corresponding HTML file. That file should be called lab9_report_A0MMMMMMM.html. Place these two files in the metaprogramming directory.
metaprogramming directory. Call this file metaprogramming.tgz. From a terminal, you can use the following command to create this file (make sure to run it at the same level where the metaprogramming folder resides):
tar czf metaprogramming.tgz metaprogramming
Due date is Tuesday, April 10.
This activity will be evaluated using the following criteria:
| 50% | Implementation of functional requirements. |
|---|---|
| 50% | Lab report. |
| DA | The program and/or report was plagiarized. |