Module: Footing::String

Defined in:
lib/extensions/string.rb

Instance Method Summary collapse

Instance Method Details

#escape(*chars) ⇒ Object

Escapes a series of chars in the current string. NOTE: A new string is returned.



27
28
29
30
31
# File 'lib/extensions/string.rb', line 27

def escape(*chars)
  gsub(/(?<!\\)(#{chars.join("|")})/) do |char|
    "\\" + char
  end
end

#humanizeObject

Converts a word with underscores into a sentance with a capitalized first word.



34
35
36
# File 'lib/extensions/string.rb', line 34

def humanize
  self.downcase.gsub(/_/, " ").capitalize
end

#random(length = 12, opts = {}) ⇒ String

Generates a random string.

Examples:

Footing.util! Footing::String
Footing::String.random_key(100, :reject => ["1", "I", "l", "0", "O"])

Parameters:

  • length (Integer) (defaults to: 12)

    The length of the key to generate. Defaults to 12.

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :reject (Array)

    A list of characters to omit from the key.

  • :upcase (Boolean)

    Indicates that only use uppercase characters should be used.

Returns:



15
16
17
18
19
20
21
22
23
# File 'lib/extensions/string.rb', line 15

def random(length=12, opts={})
  @chars ||= [(0..9).to_a, ('a'..'z').to_a, ('A'..'Z').to_a].flatten.map { |c| c.to_s }
  chars = @chars.reject do |c|
    c =~ /[a-z]/ if opts[:upcase]
  end
  opts[:reject] ||= []
  opts[:reject] = opts[:reject].map { |c| c.to_s }
  (1..length).map{ |i| (chars - opts[:reject]).sample }.join
end

#titleizeObject Also known as: titlecase

Similar to humanize but it capitalizes each word



39
40
41
# File 'lib/extensions/string.rb', line 39

def titleize
  self.split('_').map(&:capitalize).join(' ')
end