view utils.rb @ 0:1eef88068f9f tip

initial commit of maze game source
author ferencd
date Sun, 15 Sep 2019 11:46:47 +0200
parents
children
line wrap: on
line source
#
# Class to handle all kind of utilities
#

class Utils

  # Generates a random string from a set of easily readable characters
  def Utils.random_string(size = 6)
    charset = %w{0 1 2 3 4 5 6 7 9 A C D E F G H J K M N P Q R T V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z}
    (0...size).map{ charset.to_a[rand(charset.size)] }.join
  end

  # Generates a random string from a set of easily readable characters
  def Utils.random_hex_string(size = 6)
    charset = %w{0 1 2 3 4 5 6 7 9 A C D E F}
    (0...size).map{ charset.to_a[rand(charset.size)] }.join
  end

  #
  # Splits the cells
  #
  def Utils.split(text, width)
    pattern = /
    (                 # capturing group for split
      .{#{width-2},}? # at least width-2 characters, but not more than needed
      \}              # closing curly brace
      [,\]]           # a comma or a closing bracket
    )
    /x                # free spacing mode
    text.split(pattern).reject(&:empty?).join("\n")
  end

end