class String

Constants

SPACE_CHAR
TAB_CHAR

Public Instance Methods

__chars_count_for_indent(indent, tabsize) click to toggle source
# File lib/bade/ruby_extensions/string.rb, line 36
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?() click to toggle source
# File lib/bade/ruby_extensions/string.rb, line 16
def blank?
  strip.empty?
end
get_indent(tabsize) click to toggle source

Calculate indent for line

@param [Int] tabsize

@return [Int] indent size

# File lib/bade/ruby_extensions/string.rb, line 82
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) click to toggle source
# File lib/bade/ruby_extensions/string.rb, line 28
def remove_first(count = 1)
  slice(count, length - count)
end
remove_first!(count = 1) click to toggle source
# File lib/bade/ruby_extensions/string.rb, line 32
def remove_first!(count = 1)
  slice!(0, count)
end
remove_indent(indent, tabsize) click to toggle source

Remove indent

@param [Int] indent @param [Int] tabsize

# File lib/bade/ruby_extensions/string.rb, line 63
def remove_indent(indent, tabsize)
  remove_first(__chars_count_for_indent(indent, tabsize))
end
remove_indent!(indent, tabsize) click to toggle source

Remove indent

@param [Int] indent @param [Int] tabsize

# File lib/bade/ruby_extensions/string.rb, line 72
def remove_indent!(indent, tabsize)
  remove_first!(__chars_count_for_indent(indent, tabsize))
end
remove_last(count = 1) click to toggle source
# File lib/bade/ruby_extensions/string.rb, line 20
def remove_last(count = 1)
  slice(0, length - count)
end
remove_last!(count = 1) click to toggle source
# File lib/bade/ruby_extensions/string.rb, line 24
def remove_last!(count = 1)
  slice!(length - count, count)
end
single_quote() click to toggle source

Creates new string surrounded by single quotes

@return [String]

# File lib/bade/ruby_extensions/string.rb, line 12
def single_quote
  %('#{self}')
end
strip_heredoc() click to toggle source

source: apidock.com/rails/String/strip_heredoc @return [String]

# File lib/bade/ruby_extensions/string.rb, line 101
def strip_heredoc
  min_val = scan(/^[ \t]*(?=\S)/).min
  indent = (min_val && min_val.size) || 0
  gsub(/^[ \t]{#{indent}}/, '')
end