annotate utils.rb @ 0:1eef88068f9f tip

initial commit of maze game source
author ferencd
date Sun, 15 Sep 2019 11:46:47 +0200
parents
children
rev   line source
ferencd@0 1 #
ferencd@0 2 # Class to handle all kind of utilities
ferencd@0 3 #
ferencd@0 4
ferencd@0 5 class Utils
ferencd@0 6
ferencd@0 7 # Generates a random string from a set of easily readable characters
ferencd@0 8 def Utils.random_string(size = 6)
ferencd@0 9 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}
ferencd@0 10 (0...size).map{ charset.to_a[rand(charset.size)] }.join
ferencd@0 11 end
ferencd@0 12
ferencd@0 13 # Generates a random string from a set of easily readable characters
ferencd@0 14 def Utils.random_hex_string(size = 6)
ferencd@0 15 charset = %w{0 1 2 3 4 5 6 7 9 A C D E F}
ferencd@0 16 (0...size).map{ charset.to_a[rand(charset.size)] }.join
ferencd@0 17 end
ferencd@0 18
ferencd@0 19 #
ferencd@0 20 # Splits the cells
ferencd@0 21 #
ferencd@0 22 def Utils.split(text, width)
ferencd@0 23 pattern = /
ferencd@0 24 ( # capturing group for split
ferencd@0 25 .{#{width-2},}? # at least width-2 characters, but not more than needed
ferencd@0 26 \} # closing curly brace
ferencd@0 27 [,\]] # a comma or a closing bracket
ferencd@0 28 )
ferencd@0 29 /x # free spacing mode
ferencd@0 30 text.split(pattern).reject(&:empty?).join("\n")
ferencd@0 31 end
ferencd@0 32
ferencd@0 33 end