Difference between revisions of "Powers Of 2 Range Enumerable Mixin Assignment"
Jump to navigation
Jump to search
| Line 15: | Line 15: | ||
=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}} | ||
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.
Contents
Documentation To Invesigate
Implementation To Investigate (If Interested)
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 |
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 |
note: ensure that you have removed all printing to receive credit for any assignment.