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

From CSE425S Wiki
Jump to navigation Jump to search
(Created page with "In this studio we will evolve our code from our Powers of 2 exercise to leverage class mixins.")
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
In this studio we will evolve our code from our [[Powers_Of_2_Range_Enumerable_Mixin_Assignment|Powers of 2]] exercise to leverage class mixins.
+
In this studio we will evolve our code from our [[Powers_Of_2_Range_Assignment|Powers of 2]] exercise to leverage class mixins.
 +
=Documentation To Invesigate=
 +
: [https://ruby-doc.org/core-3.1.1/Enumerable.html Enumerable]
 +
 
 +
=Implementation To Investigate (If Interested)=
 +
: [https://github.com/ruby/ruby/blob/master/enum.c enum.c]
 +
 
 +
=Code To Modify=
 +
Add
 +
 
 +
include Enumerable
 +
 
 +
to your PowersOfTwoRange class definition.
 +
 
 +
=Client=
 +
{{RubyToRun|powers_of_two_range_enumerable_mixin_client|powers_of_two_range_enumerable_mixin_client|main}}
 +
 
 +
==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.