Difference between revisions of "Powers Of 2 Range Enumerable Mixin Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
 
Add
 
Add
  
  <nowiki>include Enumerable
+
  include Enumerable
</nowiki>
 
  
 
to your PowersOfTwoRange class definition.
 
to your PowersOfTwoRange class definition.
  
 
=Client=
 
=Client=
 +
{{RubyToRun|powers_of_two_range_enumerable_mixin_client|powers_of_two_range_enumerable_mixin_client|main}}
  
=Test=
+
==map example==
 +
<nowiki>texts = exclusive_powers_of_two.map do |value|
 +
  "*** #{value} ***"
 +
end
 +
p texts</nowiki>
 +
 
 +
should output:
 +
 
 +
["*** 1 ***", "*** 2 ***", "*** 4 ***", "*** 8 ***", "*** 16 ***", "*** 32 ***", "*** 64 ***", "*** 128 ***", "*** 256 ***"]
 +
 
 +
==select example==
 +
 
 +
<nowiki>perfect_squares = exclusive_powers_of_two.select do |value|
 +
  square_root = Math.sqrt(value)
 +
  square_root == square_root.to_i
 +
end
 +
p perfect_squares</nowiki>
 +
 
 +
should output
 +
 
 +
[1, 4, 16, 64, 256]
 +
 
 +
=Unit Test=
 +
{{RubyUnitTest|powers_of_two_range_enumerable_mixin|powers_of_two_range_enumerable_mixin}}

Latest revision as of 17:15, 11 April 2022

In this studio we will evolve our code from our Powers of 2 exercise to leverage class mixins.

Documentation To Invesigate

Enumerable

Implementation To Investigate (If Interested)

enum.c

Code To Modify

Add

include Enumerable

to your PowersOfTwoRange class definition.

Client

file to run: src/main/ruby/powers_of_two_range_enumerable_mixin_client/powers_of_two_range_enumerable_mixin_client.rb Ruby logo.svg

map example

texts = exclusive_powers_of_two.map do |value|
  "*** #{value} ***"
end
p texts

should output:

["*** 1 ***", "*** 2 ***", "*** 4 ***", "*** 8 ***", "*** 16 ***", "*** 32 ***", "*** 64 ***", "*** 128 ***", "*** 256 ***"]

select example

perfect_squares = exclusive_powers_of_two.select do |value|
  square_root = Math.sqrt(value)
  square_root == square_root.to_i
end
p perfect_squares

should output

[1, 4, 16, 64, 256]

Unit Test

file: src/test/ruby/powers_of_two_range_enumerable_mixin/powers_of_two_range_enumerable_mixin.rb Ruby logo.svg UnitTest

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