Difference between revisions of "Powers Of 2 Range Assignment"
Jump to navigation
Jump to search
(Created page with "=Reference= ==Ruby== : [https://apidock.com/ruby/Kernel/block_given%3F block_given?] : [https://apidock.com/ruby/Proc/yield yield] =Code to Implement= ==PowersOfTwoRange== {{...") |
|||
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Reference= | =Reference= | ||
− | + | : [https://apidock.com/ruby/Proc/yield yield] | |
: [https://apidock.com/ruby/Kernel/block_given%3F block_given?] | : [https://apidock.com/ruby/Kernel/block_given%3F block_given?] | ||
− | : [https:// | + | : [https://www.rubydoc.info/stdlib/core/Object:to_enum to_enum] |
=Code to Implement= | =Code to Implement= | ||
==PowersOfTwoRange== | ==PowersOfTwoRange== | ||
− | {{RubyToImplement|powers_of_two_range|powers_of_two_range|PowersOfTwoRange|Object|initialize(maximum, inclusive: false)<br/>each()}} | + | {{RubyToImplement|powers_of_two_range|powers_of_two_range|PowersOfTwoRange|Object|initialize(maximum, inclusive: false)<br/>each<br/>each_with_index}} |
+ | ===initialize(maximum, inclusive: false)=== | ||
+ | hang onto enough information in instance variables to support each and each_with_index | ||
+ | ===each=== | ||
+ | if a block is given, repeatedly yield with appropriate value. | ||
+ | |||
+ | otherwise, return an enum via to_enum. | ||
+ | |||
+ | ====exclusive client with block given==== | ||
+ | <nowiki>exclusive_powers_of_two = PowersOfTwoRange.new(64) | ||
+ | exclusive_powers_of_two.each do |n| | ||
+ | puts n | ||
+ | end</nowiki> | ||
+ | |||
+ | outputs | ||
+ | |||
+ | <nowiki>1 | ||
+ | 2 | ||
+ | 4 | ||
+ | 8 | ||
+ | 16 | ||
+ | 32</nowiki> | ||
+ | |||
+ | ====inclusive client with block given==== | ||
+ | |||
+ | <nowiki>exclusive_powers_of_two = PowersOfTwoRange.new(64, inclusive: true) | ||
+ | exclusive_powers_of_two.each do |n| | ||
+ | puts n | ||
+ | end</nowiki> | ||
+ | |||
+ | outputs | ||
+ | |||
+ | <nowiki>1 | ||
+ | 2 | ||
+ | 4 | ||
+ | 8 | ||
+ | 16 | ||
+ | 32 | ||
+ | 64</nowiki> | ||
+ | |||
+ | ====exclusive client without block==== | ||
+ | |||
+ | <nowiki>exclusive_powers_of_two = PowersOfTwoRange.new(64) | ||
+ | as_enum = exclusive_powers_of_two.each | ||
+ | p as_enum.to_a</nowiki> | ||
+ | |||
+ | outputs | ||
+ | |||
+ | <nowiki>[1, 2, 4, 8, 16, 32] | ||
+ | </nowiki> | ||
+ | |||
+ | ===each_with_index=== | ||
+ | if a block is given, repeatedly yield with appropriate value and its index. | ||
+ | |||
+ | otherwise, return an enum via to_enum. | ||
+ | ====exclusive client with block given==== | ||
+ | |||
+ | <nowiki>exclusive_powers_of_two = PowersOfTwoRange.new(64) | ||
+ | exclusive_powers_of_two.each_with_index do |n, index| | ||
+ | puts "#{n}, #{index}" | ||
+ | end</nowiki> | ||
+ | |||
+ | outputs | ||
+ | |||
+ | <nowiki>1, 0 | ||
+ | 2, 1 | ||
+ | 4, 2 | ||
+ | 8, 3 | ||
+ | 16, 4 | ||
+ | 32, 5</nowiki> | ||
+ | |||
+ | =Client= | ||
+ | {{RubyToRun|powers_of_two_range_client|powers_of_two_range_client|main}} | ||
− | + | =Unit Test= | |
{{RubyUnitTest|powers_of_two_range|powers_of_two_range}} | {{RubyUnitTest|powers_of_two_range|powers_of_two_range}} |
Latest revision as of 01:12, 9 March 2023
Reference
Code to Implement
PowersOfTwoRange
file: | src/main/ruby/powers_of_two_range/powers_of_two_range.rb | |
class: | PowersOfTwoRange | |
superclass: | Object | |
methods: | initialize(maximum, inclusive: false) each each_with_index |
initialize(maximum, inclusive: false)
hang onto enough information in instance variables to support each and each_with_index
each
if a block is given, repeatedly yield with appropriate value.
otherwise, return an enum via to_enum.
exclusive client with block given
exclusive_powers_of_two = PowersOfTwoRange.new(64) exclusive_powers_of_two.each do |n| puts n end
outputs
1 2 4 8 16 32
inclusive client with block given
exclusive_powers_of_two = PowersOfTwoRange.new(64, inclusive: true) exclusive_powers_of_two.each do |n| puts n end
outputs
1 2 4 8 16 32 64
exclusive client without block
exclusive_powers_of_two = PowersOfTwoRange.new(64) as_enum = exclusive_powers_of_two.each p as_enum.to_a
outputs
[1, 2, 4, 8, 16, 32]
each_with_index
if a block is given, repeatedly yield with appropriate value and its index.
otherwise, return an enum via to_enum.
exclusive client with block given
exclusive_powers_of_two = PowersOfTwoRange.new(64) exclusive_powers_of_two.each_with_index do |n, index| puts "#{n}, #{index}" end
outputs
1, 0 2, 1 4, 2 8, 3 16, 4 32, 5
Client
file to run: | src/main/ruby/powers_of_two_range_client/powers_of_two_range_client.rb |
Unit Test
file: | src/test/ruby/powers_of_two_range/powers_of_two_range.rb | UnitTest |
note: ensure that you have removed all printing to receive credit for any assignment.