Module: Footing::String
- Defined in:
- lib/extensions/string.rb
Instance Method Summary collapse
-
#escape(*chars) ⇒ Object
Escapes a series of chars in the current string.
-
#humanize ⇒ Object
Converts a word with underscores into a sentance with a capitalized first word.
-
#random(length = 12, opts = {}) ⇒ String
Generates a random string.
-
#titleize ⇒ Object
(also: #titlecase)
Similar to humanize but it capitalizes each word.
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 |
#humanize ⇒ Object
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.
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 |
#titleize ⇒ Object 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 |