class String

Constants

HTML_ESCAPE_MAP

Public Instance Methods

camel_case() click to toggle source

camel_case('snake_case') # => snakeCase.

# File lib/sublime_dsl/core_ext/string.rb, line 52
def camel_case
  self.gsub(/_(.)/) { $1.upcase }
end
dedent() click to toggle source

Shifts all lines left, preserving relative indentation.

# File lib/sublime_dsl/core_ext/string.rb, line 66
def dedent
  self.dup.dedent!
end
dedent!() click to toggle source

Shifts all lines left, preserving relative indentation. Returns self.

# File lib/sublime_dsl/core_ext/string.rb, line 72
def dedent!
  if (re = dedent_regexp)
    self.gsub! re, ''
  end
  self
end
html_escape(escape_quotes = true) click to toggle source

Returns the string with characters escaped to HTML entities:

< => &lt;
> => &gt;
& => &amp;
" => &quot;  (if escape_quotes == true)
# File lib/sublime_dsl/core_ext/string.rb, line 38
def html_escape(escape_quotes = true)
  if escape_quotes
    self.gsub(/[&><"]/, HTML_ESCAPE_MAP)
  else
    self.gsub(/[&><]/, HTML_ESCAPE_MAP)
  end
end
indent(spaces) click to toggle source

Indents all non-empty lines by the number of spaces passed.

# File lib/sublime_dsl/core_ext/string.rb, line 80
def indent(spaces)
  self.gsub(/^(.+)/, (' ' * spaces) + '\1')
end
indent!(spaces) click to toggle source

Indents all non-empty lines by the number of spaces passed. Returns self.

# File lib/sublime_dsl/core_ext/string.rb, line 86
def indent!(spaces)
  self.gsub!(/^(.+)/, (' ' * spaces) + '\1')
  self
end
inspect_dq() click to toggle source

Same as inspect, but only escapes control characters and double quotes.

# File lib/sublime_dsl/core_ext/string.rb, line 14
def inspect_dq
  '"' << self.gsub(/[[:cntrl:]\\"]/) { |c| c.inspect[1..-2]  } << '"'
end
inspect_sq() click to toggle source

Same as inspect, but between single quotes, so only \ and single quotes are escaped.

# File lib/sublime_dsl/core_ext/string.rb, line 7
def inspect_sq
  # escape all \ trailing, or followed by a \ or a '
  # escape single quotes
  "'" << self.gsub(/\\$|\\(?=[\\'])/, '\\\\\\\\').gsub("'", "\\\\'") << "'"
end
pascal_case() click to toggle source

pascal_case('snake_case') # => SnakeCase.

# File lib/sublime_dsl/core_ext/string.rb, line 47
def pascal_case
  self.gsub(/(?:^|_)(.)/) { $1.upcase }
end
snake_case() click to toggle source

snake_case('PascalCase') # => pascal_case. snake_case('camelCase') # => camel_case.

# File lib/sublime_dsl/core_ext/string.rb, line 58
def snake_case
  self
    .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
    .gsub(/([a-z\d])([A-Z])/,'\1_\2')
    .downcase
end
to_source(prefer_dq = false) click to toggle source

Returns the shortest between inspect_sq and inspect_dq. If same length, acts according to prefer_dq (prefer double quotes?)

# File lib/sublime_dsl/core_ext/string.rb, line 21
def to_source(prefer_dq = false)
  sq = inspect_sq
  dq = inspect_dq
  case dq.length <=> sq.length
  when  1 then sq
  when -1 then dq
  when  0 then prefer_dq ? dq : sq
  end
end
wrap(width = 80) click to toggle source

Wraps at spaces so that no line is longer than the passed width, if possible. Multiple newlines, tabs & spaces are replaced by a single space.

# File lib/sublime_dsl/core_ext/string.rb, line 94
def wrap(width = 80)
  words = self.split(/\s+/m)
  wrapped = words.shift
  len = wrapped.length
  words.each do |w|
    len += 1 + w.length
    if len > width
      wrapped << "\n" << w
      len = w.length
    else
      wrapped << ' ' << w
    end
  end
  wrapped
end

Private Instance Methods

dedent_regexp() click to toggle source

Returns the regexp to dedent self, or nil if no dedent is needed, or there are leading tabs on non-empty lines.

# File lib/sublime_dsl/core_ext/string.rb, line 114
def dedent_regexp
  min_indent = self.length
  self.each_line do |line|
    # if no leading space, no dedent
    line =~ /^\S/ and return nil
    # skip empty lines
    line =~ /^\s*$/ and next
    # do nothing when tabs are present
    line =~ /^ *\t/ and return nil
    indent = line.match(/^ +/).to_s.length
    min_indent = indent if indent < min_indent
  end
  Regexp.new('^' << ' ' * min_indent, Regexp::MULTILINE)
end