Class: String

Inherits:
Object show all
Defined in:
lib/bade/ruby_extensions/string.rb

Overview

:nodoc:

Constant Summary collapse

SPACE_CHAR =
' '.freeze
TAB_CHAR =
"\t".freeze

Instance Method Summary collapse

Instance Method Details

#__chars_count_for_indent(indent, tabsize) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/bade/ruby_extensions/string.rb', line 16

def blank?
  strip.empty?
end

#get_indent(tabsize) ⇒ Int

Calculate indent for line

Parameters:

  • tabsize (Int)

Returns:

  • (Int)

    indent size



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bade/ruby_extensions/string.rb', line 82

def get_indent(tabsize)
  count = 0

  each_char do |char|
    case char
    when SPACE_CHAR
      count += 1
    when TAB_CHAR
      count += tabsize
    else
      break
    end
  end

  count
end

#remove_first(count = 1) ⇒ Object



28
29
30
# File 'lib/bade/ruby_extensions/string.rb', line 28

def remove_first(count = 1)
  slice(count, length - count)
end

#remove_first!(count = 1) ⇒ Object



32
33
34
# File 'lib/bade/ruby_extensions/string.rb', line 32

def remove_first!(count = 1)
  slice!(0, count)
end

#remove_indent(indent, tabsize) ⇒ Object

Remove indent

Parameters:

  • indent (Int)
  • tabsize (Int)


63
64
65
# 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) ⇒ Object

Remove indent

Parameters:

  • indent (Int)
  • tabsize (Int)


72
73
74
# 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) ⇒ Object



20
21
22
# File 'lib/bade/ruby_extensions/string.rb', line 20

def remove_last(count = 1)
  slice(0, length - count)
end

#remove_last!(count = 1) ⇒ Object



24
25
26
# File 'lib/bade/ruby_extensions/string.rb', line 24

def remove_last!(count = 1)
  slice!(length - count, count)
end

#single_quoteString

Creates new string surrounded by single quotes

Returns:



12
13
14
# File 'lib/bade/ruby_extensions/string.rb', line 12

def single_quote
  %('#{self}')
end