Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/bade/ruby_extensions/string.rb
Overview
:nodoc:
Constant Summary collapse
- SPACE_CHAR =
' '.freeze
- TAB_CHAR =
"\t".freeze
Instance Method Summary collapse
- #__chars_count_for_indent(indent, tabsize) ⇒ Object
- #blank? ⇒ Boolean
-
#get_indent(tabsize) ⇒ Int
Calculate indent for line.
- #remove_first(count = 1) ⇒ Object
- #remove_first!(count = 1) ⇒ Object
-
#remove_indent(indent, tabsize) ⇒ Object
Remove indent.
-
#remove_indent!(indent, tabsize) ⇒ Object
Remove indent.
- #remove_last(count = 1) ⇒ Object
- #remove_last!(count = 1) ⇒ Object
-
#single_quote ⇒ String
Creates new string surrounded by single quotes.
- #strip_heredoc ⇒ String
Instance Method Details
#__chars_count_for_indent(indent, tabsize) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bade/ruby_extensions/string.rb', line 39 def __chars_count_for_indent(indent, tabsize) count = 0 each_char do |char| break if indent <= 0 case char when SPACE_CHAR indent -= 1 when TAB_CHAR raise StandardError, 'malformed tabs' if indent - tabsize < 0 indent -= tabsize else break end count += 1 end count end |
#blank? ⇒ Boolean
17 18 19 |
# File 'lib/bade/ruby_extensions/string.rb', line 17 def blank? strip.empty? end |
#get_indent(tabsize) ⇒ Int
Calculate indent for line
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bade/ruby_extensions/string.rb', line 87 def get_indent(tabsize) count = 0 each_char do |char| if char == SPACE_CHAR count += 1 elsif char == TAB_CHAR count += tabsize else break end end count end |
#remove_first(count = 1) ⇒ Object
31 32 33 |
# File 'lib/bade/ruby_extensions/string.rb', line 31 def remove_first(count = 1) slice(count, length - count) end |
#remove_first!(count = 1) ⇒ Object
35 36 37 |
# File 'lib/bade/ruby_extensions/string.rb', line 35 def remove_first!(count = 1) slice!(0, count) end |
#remove_indent(indent, tabsize) ⇒ Object
Remove indent
66 67 68 |
# File 'lib/bade/ruby_extensions/string.rb', line 66 def remove_indent(indent, tabsize) remove_first(__chars_count_for_indent(indent, tabsize)) end |
#remove_indent!(indent, tabsize) ⇒ Object
Remove indent
76 77 78 |
# File 'lib/bade/ruby_extensions/string.rb', line 76 def remove_indent!(indent, tabsize) remove_first!(__chars_count_for_indent(indent, tabsize)) end |
#remove_last(count = 1) ⇒ Object
22 23 24 |
# File 'lib/bade/ruby_extensions/string.rb', line 22 def remove_last(count = 1) slice(0, length - count) end |
#remove_last!(count = 1) ⇒ Object
26 27 28 |
# File 'lib/bade/ruby_extensions/string.rb', line 26 def remove_last!(count = 1) slice!(length - count, count) end |
#single_quote ⇒ String
Creates new string surrounded by single quotes
12 13 14 |
# File 'lib/bade/ruby_extensions/string.rb', line 12 def single_quote %('#{self}') end |
#strip_heredoc ⇒ String
106 107 108 109 110 |
# File 'lib/bade/ruby_extensions/string.rb', line 106 def strip_heredoc min_val = scan(/^[ \t]*(?=\S)/).min indent = (min_val && min_val.size) || 0 gsub(/^[ \t]{#{indent}}/, '') end |