class AsciiParadise::SierpinksiTriangle

Constants

N_TIMES
#

N_TIMES

#

Public Class Methods

new( optional_input = ARGV, run_already = true ) click to toggle source
#

initialize

#
# File lib/ascii_paradise/static_ascii/sierpinksi_triangle.rb, line 26
def initialize(
    optional_input = ARGV,
    run_already    = true
  )
  reset
  handle_input(
    optional_input
  )
  run if run_already
end

Public Instance Methods

handle_input(i) click to toggle source
#

handle_input

This method will handle input given to this class.

#
# File lib/ascii_paradise/static_ascii/sierpinksi_triangle.rb, line 42
def handle_input(i)
  if i
    if i.is_a?(Array) and !i.empty?
      @n_times = i.first
    elsif i.is_a?(Array) and i.empty? # Array is empty, thus use the default.
      @n_times = N_TIMES
    elsif i.is_a? Hash
      if i.has_key? :size
        @n_times = i.delete :size
      end
    else
      @n_times = i
    end
    @n_times = @n_times.to_i
  end
end
reset() click to toggle source
#

reset

#
# File lib/ascii_paradise/static_ascii/sierpinksi_triangle.rb, line 62
def reset
  @n_times = N_TIMES
end
run() click to toggle source
#

run

#
# File lib/ascii_paradise/static_ascii/sierpinksi_triangle.rb, line 69
def run
  @n_times.times { |counter|
    joined_substring = (0 .. counter).map {|entry|
      # =================================================================== #
      # Either add a ' .' token or a ' A' token.
      # =================================================================== #
      ~counter & entry > 0 ? ' .' : ' A'
    }.join
    print ' ' * (@n_times-1-counter), joined_substring
    e
  }
end