Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/lydown/core_ext.rb

Constant Summary collapse

UNESCAPES =

String unescaping code from here: stackoverflow.com/a/20131717

{
    'a' => "\x07", 'b' => "\x08", 't' => "\x09",
    'n' => "\x0a", 'v' => "\x0b", 'f' => "\x0c",
    'r' => "\x0d", 'e' => "\x1b", "\\\\" => "\x5c",
    "\"" => "\x22", "'" => "\x27"
}

Instance Method Summary collapse

Instance Method Details

#camelizeObject



108
109
110
# File 'lib/lydown/core_ext.rb', line 108

def camelize
  split('_').collect(&:capitalize).join
end

#titlize(all_capitals = false) ⇒ Object



102
103
104
105
106
# File 'lib/lydown/core_ext.rb', line 102

def titlize(all_capitals = false)
  all_capitals ? 
    self.gsub("-", " ").gsub(/\b('?[a-z])/) {$1.capitalize} :
    self.gsub("-", " ").capitalize
end

#unescapeObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lydown/core_ext.rb', line 121

def unescape
  # Escape all the things
  gsub(/\\(?:([#{UNESCAPES.keys.join}])|u([\da-fA-F]{4}))|\\0?x([\da-fA-F]{2})/) {
    if $1
      if $1 == '\\' then '\\' else UNESCAPES[$1] end
    elsif $2 # escape \u0000 unicode
      ["#$2".hex].pack('U*')
    elsif $3 # escape \0xff or \xff
      [$3].pack('H2')
    end
  }
end