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