Difference between revisions of "Render Part D Assignment"
Line 7: | Line 7: | ||
=Code to Implement= | =Code to Implement= | ||
==Composite== | ==Composite== | ||
+ | {{RubyModuleToImplement|composite|render/assignment|Composite|initialize_components<br/>push_component(component)<br/>each_component}} | ||
+ | |||
module Composite should define methods push_component and each_component as well as include the [https://ruby-doc.org/core-2.6.3/Enumerable.html Enumerable] module. | module Composite should define methods push_component and each_component as well as include the [https://ruby-doc.org/core-2.6.3/Enumerable.html Enumerable] module. | ||
===push_component=== | ===push_component=== |
Revision as of 04:40, 8 December 2020
In this studio we will evolve our code from Render_Part_C_Assignment to leverage mixins.
Continue editing files in the render/assignment directory.
Contents
Code to Implement
Composite
file: | src/main/ruby/render/assignment/composite.rb | |
module: | Composite | |
methods: | initialize_components push_component(component) each_component |
module Composite should define methods push_component and each_component as well as include the Enumerable module.
push_component
push a component onto an array.
each_component
if a block is given, yield for each component. otherwise return a new Enumerator of the components.
Enumerable
to add a number of useful methods, include the Enumerable module and use alias_method to alias an :each
method to the :each_component
method.
CompositeTransform
Mixin your Composite module to the CompositeTransform class and clean up existing code.
include Composite
Warning: Do NOT use the include
keyword in Ruby as you would typically in C or C++. Be sure to include within the intended class definition. If you include outside of the class at the top-level, it will mixin Composite into Object. This may or may not rip a hole in the universe.
Scene
include Composite and clean up existing code.
Testing Your Solution
Unit Test
file: | src/test/ruby/render/part_d/part_d_unit_test.rb | UnitTest |
note: ensure that you have removed all printing to receive credit for any assignment.