Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/sublime_dsl/core_ext/string.rb
Constant Summary collapse
- HTML_ESCAPE_MAP =
{ '&' => '&', '"' => '"', '<' => '<', '>' => '>' }
Instance Method Summary collapse
-
#camel_case ⇒ Object
camel_case('snake_case') # => snakeCase. -
#dedent ⇒ Object
Shifts all lines left, preserving relative indentation.
-
#dedent! ⇒ Object
Shifts all lines left, preserving relative indentation.
-
#html_escape(escape_quotes = true) ⇒ Object
Returns the string with characters escaped to HTML entities: < => < > => > & => & “ => " (if escape_quotes == true).
-
#indent(spaces) ⇒ Object
Indents all non-empty lines by the number of spaces passed.
-
#indent!(spaces) ⇒ Object
Indents all non-empty lines by the number of spaces passed.
-
#inspect_dq ⇒ Object
Same as #inspect, but only escapes control characters and double quotes.
-
#inspect_sq ⇒ Object
Same as #inspect, but between single quotes, so only \ and single quotes are escaped.
-
#pascal_case ⇒ Object
pascal_case('snake_case') # => SnakeCase. -
#snake_case ⇒ Object
snake_case('PascalCase') # => pascal_case. -
#to_source(prefer_dq = false) ⇒ Object
Returns the shortest between #inspect_sq and #inspect_dq.
-
#wrap(width = 80) ⇒ Object
Wraps at spaces so that no line is longer than the passed
width, if possible.
Instance Method Details
#camel_case ⇒ Object
camel_case('snake_case') # => snakeCase.
52 53 54 |
# File 'lib/sublime_dsl/core_ext/string.rb', line 52 def camel_case self.gsub(/_(.)/) { $1.upcase } end |
#dedent ⇒ Object
Shifts all lines left, preserving relative indentation.
66 67 68 |
# File 'lib/sublime_dsl/core_ext/string.rb', line 66 def dedent self.dup.dedent! end |
#dedent! ⇒ Object
Shifts all lines left, preserving relative indentation. Returns self.
72 73 74 75 76 77 |
# 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) ⇒ Object
Returns the string with characters escaped to HTML entities:
< => <
> => >
& => &
" => " (if escape_quotes == true)
38 39 40 41 42 43 44 |
# 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) ⇒ Object
Indents all non-empty lines by the number of spaces passed.
80 81 82 |
# File 'lib/sublime_dsl/core_ext/string.rb', line 80 def indent(spaces) self.gsub(/^(.+)/, (' ' * spaces) + '\1') end |
#indent!(spaces) ⇒ Object
Indents all non-empty lines by the number of spaces passed. Returns self.
86 87 88 89 |
# File 'lib/sublime_dsl/core_ext/string.rb', line 86 def indent!(spaces) self.gsub!(/^(.+)/, (' ' * spaces) + '\1') self end |
#inspect_dq ⇒ Object
Same as #inspect, but only escapes control characters and double quotes.
14 15 16 |
# File 'lib/sublime_dsl/core_ext/string.rb', line 14 def inspect_dq '"' << self.gsub(/[[:cntrl:]\\"]/) { |c| c.inspect[1..-2] } << '"' end |
#inspect_sq ⇒ Object
Same as #inspect, but between single quotes, so only \ and single quotes are escaped.
7 8 9 10 11 |
# 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 ⇒ Object
pascal_case('snake_case') # => SnakeCase.
47 48 49 |
# File 'lib/sublime_dsl/core_ext/string.rb', line 47 def pascal_case self.gsub(/(?:^|_)(.)/) { $1.upcase } end |
#snake_case ⇒ Object
snake_case('PascalCase') # => pascal_case. snake_case('camelCase') # => camel_case.
58 59 60 61 62 63 |
# 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) ⇒ Object
Returns the shortest between #inspect_sq and #inspect_dq. If same length, acts according to prefer_dq (prefer double quotes?)
21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# 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 |