class String
Public Class Methods
generate_token(options = {})
click to toggle source
Returns a psuedo-random UUID which sepearated by hyphens in standard UUID format. It's worth noting this doesn't actually follow the specification for Version 4 UUIDs.
# File lib/maini/utils/extensions/string.rb, line 55 def generate_token(options = {}) values = [rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x1000000), rand(0x1000000)] "%04x%04x-%04x-%04x-%04x-%06x%06x" % values end
random(options = {})
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 48 def random(options = {}) Maini::Utils::RandomString.generate(options) end
Public Instance Methods
ansi(code)
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 30 def ansi(code) "\e[#{code.to_s}m#{self}\e[0m" end
bg_black()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 19 def bg_black; ansi(40) end
bg_blue()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 23 def bg_blue; ansi(44) end
bg_brown()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 22 def bg_brown; ansi(43) end
bg_cyan()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 25 def bg_cyan; ansi(46) end
bg_gray()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 26 def bg_gray; ansi(47) end
bg_green()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 21 def bg_green; ansi(42) end
bg_magenta()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 24 def bg_magenta; ansi(45) end
bg_red()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 20 def bg_red; ansi(41) end
black()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 11 def black; ansi(30) end
blue()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 15 def blue; ansi(34) end
bold()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 27 def bold; "\033[1m#{self}\033[22m" end
brown()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 14 def brown; ansi(33) end
cyan()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 17 def cyan; ansi(36) end
gray()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 18 def gray; ansi(37) end
green()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 13 def green; ansi(32) end
magenta()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 16 def magenta; ansi(35) end
red()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 12 def red; ansi(31) end
reverse_color()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 28 def reverse_color; "\033[7m#{self}\033[27m" end
to_bool()
click to toggle source
# File lib/maini/utils/extensions/string.rb, line 5 def to_bool return true if ['true', '1', 'yes', 'on', 't'].include? self return false if ['false', '0', 'no', 'off', 'f'].include? self return nil end
to_md5(length = 32)
click to toggle source
Returns the hex-encoded MD5 hash value of the current string. Optionally, pass a value to limit the length of the returned SHA.
# File lib/maini/utils/extensions/string.rb, line 42 def to_md5(length = 32) Digest::MD5.hexdigest(self)[0,length] end
to_sha1(length = 40)
click to toggle source
Returns the hex-encoded SHA1 hash value of the current string. Optionally, pass a value to limit the length of the returned SHA.
# File lib/maini/utils/extensions/string.rb, line 36 def to_sha1(length = 40) Digest::SHA1.hexdigest(self)[0,length] end