Class: String
Instance Method Summary collapse
-
#get_indent(tabsize) ⇒ Int
Calculate indent for line.
-
#remove_indent(indent, tabsize) ⇒ Object
Remove indent.
-
#remove_indent!(indent, tabsize) ⇒ Object
Remove indent.
-
#single_quote ⇒ String
Creates new string surrounded by single quotes.
Instance Method Details
#get_indent(tabsize) ⇒ Int
Calculate indent for line
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bade/ruby_extensions/string.rb', line 58 def get_indent(tabsize) count = 0 self.each_char do |char| if char == ' ' count += 1 elsif char == "\t" count += tabsize else break end end count end |
#remove_indent(indent, tabsize) ⇒ Object
Remove indent
17 18 19 |
# File 'lib/bade/ruby_extensions/string.rb', line 17 def remove_indent(indent, tabsize) self.dup.remove_indent!(indent, tabsize) end |
#remove_indent!(indent, tabsize) ⇒ Object
Remove indent
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bade/ruby_extensions/string.rb', line 27 def remove_indent!(indent, tabsize) count = 0 self.each_char do |char| if indent <= 0 break elsif char == ' ' indent -= 1 elsif char == "\t" if indent - tabsize < 0 raise StandardError, 'malformed tabs' end indent -= tabsize else break end count += 1 end self[0 ... self.length] = self[count ... self.length] end |
#single_quote ⇒ String
Creates new string surrounded by single quotes
7 8 9 |
# File 'lib/bade/ruby_extensions/string.rb', line 7 def single_quote %('#{self}') end |