Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/bashly/extensions/string.rb
Instance Method Summary collapse
- #color(marker) ⇒ Object
- #expand_tabs(tabstop = 2) ⇒ Object
- #indent(offset) ⇒ Object
- #lint ⇒ Object
- #remove_front_matter ⇒ Object
- #sanitize_for_print ⇒ Object
- #to_path ⇒ Object
- #to_underscore ⇒ Object
- #wrap(length = 80) ⇒ Object
Instance Method Details
#color(marker) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/bashly/extensions/string.rb', line 45 def color(marker) color = Bashly::Settings.usage_colors[marker.to_s] return self unless color text, spaces = match(/(.*?)(\s*)$/).captures %[$(#{color} "#{text}")#{spaces}] end |
#expand_tabs(tabstop = 2) ⇒ Object
39 40 41 42 43 |
# File 'lib/bashly/extensions/string.rb', line 39 def (tabstop = 2) gsub(/^( {#{tabstop}}+)/) do "\t" * (::Regexp.last_match(1).size / tabstop) end end |
#indent(offset) ⇒ Object
6 7 8 9 10 |
# File 'lib/bashly/extensions/string.rb', line 6 def indent(offset) return self unless offset.positive? lines.indent(offset).join end |
#lint ⇒ Object
31 32 33 |
# File 'lib/bashly/extensions/string.rb', line 31 def lint gsub(/\s+\n/m, "\n\n").lines.grep_v(/^\s*##/).join end |
#remove_front_matter ⇒ Object
35 36 37 |
# File 'lib/bashly/extensions/string.rb', line 35 def remove_front_matter split(/^---\s*/).last end |
#sanitize_for_print ⇒ Object
2 3 4 |
# File 'lib/bashly/extensions/string.rb', line 2 def sanitize_for_print gsub("\n", '\\n').gsub('"', '\"') end |
#to_path ⇒ Object
16 17 18 |
# File 'lib/bashly/extensions/string.rb', line 16 def to_path tr(' ', '/').downcase end |
#to_underscore ⇒ Object
12 13 14 |
# File 'lib/bashly/extensions/string.rb', line 12 def to_underscore gsub(/(.)([A-Z])/, '\1_\2').gsub(/[- ]/, '_').downcase end |
#wrap(length = 80) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bashly/extensions/string.rb', line 20 def wrap(length = 80) strip! split("\n").collect! do |line| if line.length > length line.gsub(/(.{1,#{length}})(\s+|$)/, "\\1\n").rstrip else line end end * "\n" end |