diff options
| author | a14m <[email protected]> | 2020-11-30 11:41:26 +0100 |
|---|---|---|
| committer | a14m <[email protected]> | 2020-11-30 11:41:26 +0100 |
| commit | 8c70baf26570075eb362163a8a2419711168d673 (patch) | |
| tree | c94187216aa4505f4016f4adf41664c4edacf11f /CLI/ruby | |
| parent | 77543134da94fa5edb0e471e1a859b4190eb3cb0 (diff) | |
Use class notation for adding class methods
This to allow private class methods
Ref: https://medium.com/rubycademy/3-ways-to-make-class-methods-private-in-ruby-64b970e54613
Diffstat (limited to 'CLI/ruby')
| -rw-r--r-- | CLI/ruby/lib/game_of_life/generators/input.rb | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/CLI/ruby/lib/game_of_life/generators/input.rb b/CLI/ruby/lib/game_of_life/generators/input.rb index 77adc85..63a4c03 100644 --- a/CLI/ruby/lib/game_of_life/generators/input.rb +++ b/CLI/ruby/lib/game_of_life/generators/input.rb @@ -6,34 +6,37 @@ module GameOfLife module Generators # Generate a new Universe/Board with a parsed local file or remote URL/file module Input - # 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 - def self.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 + class << self - # 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:]]/) } } + # 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 + 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 - # 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[i][j] = cell + # 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:]]/) } } + + # 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[i][j] = cell + end end + universe end - universe + # rubocop:enable Metrics/AbcSize end - # rubocop:enable Metrics/AbcSize end end end |
