Difference between revisions of "Render Part D Assignment"
Line 18: | Line 18: | ||
===initialize_components=== | ===initialize_components=== | ||
+ | add an instance variable with a suitable name to store an array of components | ||
===push_component=== | ===push_component=== | ||
− | push a component onto | + | push a component onto your array instance variable. |
===each_component=== | ===each_component=== | ||
− | if a block is given, yield for each component. otherwise | + | if a block is given, yield for each component in your array instance variable. |
+ | |||
+ | otherwise use [https://ruby-doc.org/core-2.6.3/Object.html#method-i-to_enum to_enum] to return a new Enumerator of the components. | ||
Note: be sure to invoke <code>to_enum</code> with the symbol of the method <code>:each_component</code> and not invoke the method by mistake. | Note: be sure to invoke <code>to_enum</code> with the symbol of the method <code>:each_component</code> and not invoke the method by mistake. | ||
− | ===Enumerable=== | + | ===Mixin Enumerable=== |
− | to add a number of useful methods, include the [https://ruby-doc.org/core-2.6.3/Enumerable.html Enumerable] module and use [https://www.rubydoc.info/stdlib/core/Module:alias_method alias_method] to alias an <code>:each</code> method to the <code>:each_component </code> method. | + | to add a number of useful methods, include the [https://ruby-doc.org/core-2.6.3/Enumerable.html Enumerable] module and |
+ | |||
+ | ===Alias each=== | ||
+ | use [https://www.rubydoc.info/stdlib/core/Module:alias_method alias_method] to alias an <code>:each</code> method to the <code>:each_component </code> method. | ||
==CompositeTransform== | ==CompositeTransform== |
Revision as of 05:22, 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
Background
Investigate Ruby Modules, specifically the module
and <include> keywords.
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 or module definition.
If you include
at the top-level, it will mixin to Object
!
This may or may not rip a hole in the universe.
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 initialize_components
, push_component
, and each_component
as well as mixin the Enumerable module.
initialize_components
add an instance variable with a suitable name to store an array of components
push_component
push a component onto your array instance variable.
each_component
if a block is given, yield for each component in your array instance variable.
otherwise use to_enum to return a new Enumerator of the components.
Note: be sure to invoke to_enum
with the symbol of the method :each_component
and not invoke the method by mistake.
Mixin Enumerable
to add a number of useful methods, include the Enumerable module and
Alias each
use alias_method to alias an :each
method to the :each_component
method.
CompositeTransform
Mixin your Composite module to the CompositeTransform class.
include Composite
Be sure to invoke initialize_components
from the constructor and clean up existing code. For example bounds calculation can use map
.
Scene
Mixin your Composite module to the Scene class and clean up existing code.
include Composite
Testing Your Solution
Unit Test
file: | src/test/ruby/render/part_d/part_d_unit_test.rb | ![]() |
note: ensure that you have removed all printing to receive credit for any assignment.