summaryrefslogtreecommitdiffstats
path: root/CLI/ruby/lib
diff options
context:
space:
mode:
authora14m <[email protected]>2020-11-30 11:56:09 +0100
committera14m <[email protected]>2020-11-30 11:56:09 +0100
commitbb03e493c2d29e8ba01061331b034a3f4f6648a3 (patch)
tree1b0d6d2a60e84bcc1f3ca6ede18323889f8617c3 /CLI/ruby/lib
parent8c70baf26570075eb362163a8a2419711168d673 (diff)
Add the private populate method to populate the generated universe
Diffstat (limited to 'CLI/ruby/lib')
-rw-r--r--CLI/ruby/lib/game_of_life/generators/input.rb24
1 files changed, 14 insertions, 10 deletions
diff --git a/CLI/ruby/lib/game_of_life/generators/input.rb b/CLI/ruby/lib/game_of_life/generators/input.rb
index 63a4c03..7a255fd 100644
--- a/CLI/ruby/lib/game_of_life/generators/input.rb
+++ b/CLI/ruby/lib/game_of_life/generators/input.rb
@@ -7,16 +7,13 @@ module GameOfLife
# Generate a new Universe/Board with a parsed local file or remote URL/file
module Input
class << self
-
# Generate a new Universe/Board with a parsed local file or remote URL/file
# @param options [Hash] CLI options to generate initial conditions
# @option options [String] "input" path to a local file or URL to open
# @option options [Numeric] "width" (floored) and used as the width of the universe
# @option options [Numeric] "height" (floored) and used as the height of the universe
- # @return [TODO]
- # @smell
- # rubocop:disable Metrics/AbcSize as the method needs to do the reading/parsing and initialization of the
- # universe array
+ # @return [GameOfLife::Universe] populated with the parsed input
+ # @see ::GameOfLife::Generators::Input.populate
def new(options)
# By design, it's intentional that we use URI.open to to allow seamless file and url access
raw = URI.open(options["input"]).read.split("\n") # rubocop:disable Security/Open
@@ -24,18 +21,25 @@ module GameOfLife
# Parse the file and convert it to a boolean 2D array
# where any alpha numeric character is considered a living cell (true)
raw.map! { |row| row.chars.map { |char| char.match?(/[[:alnum:]]/) } }
+ populate(parsed_input: raw, width: options["width"].to_i, height: options["height"].to_i)
+ end
+ # Populate the {GameOfLife::Universe} with reference to the parsed input 2D array (True/False)
+ # @param parsed_input [Array<Array<True|False>>]
+ # @param widht [Integer] width of the generated cyclic universe
+ # @param height [Integer] height of the generated cyclic universe
+ # @return [GameOfLife::Universe] populated with the parsed input
+ private def populate(parsed_input:, width:, height:)
# Generate a universe based on the parsed raw file and fill it with the cells
- universe = GameOfLife::Universe.new(height: options["height"], width: options["width"])
- (0...options["height"].to_i).each do |i|
- (0...options["width"].to_i).each do |j|
- cell = ::GameOfLife::Cell.new(alive: raw.fetch(i, nil)&.fetch(j, nil))
+ universe = GameOfLife::Universe.new(height: height, width: width)
+ (0...height).each do |i|
+ (0...width).each do |j|
+ cell = ::GameOfLife::Cell.new(alive: parsed_input.fetch(i, nil)&.fetch(j, nil))
universe[i][j] = cell
end
end
universe
end
- # rubocop:enable Metrics/AbcSize
end
end
end