Difference between revisions of "Render Part D Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 4: Line 4:
  
 
Continue editing files in the render/assignment directory.
 
Continue editing files in the render/assignment directory.
 +
 +
=Background=
 +
[https://www.tutorialspoint.com/ruby/ruby_modules.htm Ruby Modules]
 +
 +
[[File:Warning_icon.svg|left|100px|caption]] Warning: Do '''NOT''' use the <code>include</code> keyword in Ruby as you would typically in C or C++.<br/>Be sure to include within the intended class or module definition.<br/>If you <code>include</code> at the top-level, it will mixin to <code>Object</code>!<br/>This may or may not rip a hole in the universe.
  
 
=Code to Implement=
 
=Code to Implement=
 
==Composite==
 
==Composite==
{{RubyModuleToImplement|composite|render/assignment|Composite|initialize_components<br/>push_component(component)<br/>each_component}}
+
{{RubyModuleToImplement|composite|render/assignment|Composite|initialize_components()<br/>push_component(component)<br/>each_component()}}
 +
 
 +
module Composite should define methods <code>initialize_components</code>, <code>push_component</code>, and <code>each_component</code> as well as mixin the [https://ruby-doc.org/core-2.6.3/Enumerable.html Enumerable] module.
 +
 
 +
===initialize_components===
  
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===
 
push a component onto an array.
 
push a component onto an array.
Line 15: Line 23:
 
===each_component===
 
===each_component===
 
if a block is given, yield for each component.  otherwise return [https://ruby-doc.org/core-2.6.3/Object.html#method-i-to_enum a new Enumerator] of the components.
 
if a block is given, yield for each component.  otherwise return [https://ruby-doc.org/core-2.6.3/Object.html#method-i-to_enum 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.
  
 
===Enumerable===
 
===Enumerable===
Line 24: Line 34:
 
  <code>include Composite</code>
 
  <code>include Composite</code>
  
[[File:Warning_icon.svg|100px]] Warning: Do '''NOT''' use the <code>include</code> keyword in Ruby as you would typically in C or C++.  Be sure to include within the intended class or module definition.  If you <code>include</code> at the top-level, it will mixin to <code>Object</code>!  This may or may not rip a hole in the universe.
+
==Scene==
 +
Mixin your Composite module to the Scene class and clean up existing code.
  
==Scene==
+
<code>include Composite</code>
include Composite and clean up existing code.
 
  
 
=Testing Your Solution=
 
=Testing Your Solution=
 
==Unit Test==
 
==Unit Test==
 
{{RubyUnitTest|part_d_unit_test|render/part_d}}
 
{{RubyUnitTest|part_d_unit_test|render/part_d}}

Revision as of 04:55, 8 December 2020

In this studio we will evolve our code from Render_Part_C_Assignment to leverage mixins.

Render part d class hierarchy.svg

Continue editing files in the render/assignment directory.

Background

Ruby Modules

caption

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 Ruby logo.svg
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

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.

Note: be sure to invoke to_enum with the symbol of the method :each_component and not invoke the method by mistake.

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

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 Ruby logo.svg UnitTest

note: ensure that you have removed all printing to receive credit for any assignment.