Class: String

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

Constant Summary collapse

SPACE_CHAR =
' '
TAB_CHAR =
"\t"

Instance Method Summary collapse

Instance Method Details

#__chars_count_for_indent(indent, tabsize) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bade/ruby_extensions/string.rb', line 38

def __chars_count_for_indent(indent, tabsize)
  count = 0
  self.each_char do |char|
    break if indent <= 0

    case char
    when SPACE_CHAR
      indent -= 1
    when TAB_CHAR
      if indent - tabsize < 0
        raise StandardError, 'malformed tabs'
      end

      indent -= tabsize
    else
      break
    end

    count += 1
  end

  count
end

#blank?Boolean

Returns:



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

def blank?
  strip.length == 0
end

#get_indent(tabsize) ⇒ Int

Calculate indent for line

Parameters:

Returns:

  • indent size



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bade/ruby_extensions/string.rb', line 88

def get_indent(tabsize)
  count = 0

  self.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



30
31
32
# File 'lib/bade/ruby_extensions/string.rb', line 30

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

#remove_first!(count = 1) ⇒ Object



34
35
36
# File 'lib/bade/ruby_extensions/string.rb', line 34

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

#remove_indent(indent, tabsize) ⇒ Object

Remove indent

Parameters:



67
68
69
# File 'lib/bade/ruby_extensions/string.rb', line 67

def remove_indent(indent, tabsize)
  remove_first(__chars_count_for_indent(indent, tabsize))
end

#remove_indent!(indent, tabsize) ⇒ Object

Remove indent

Parameters:



77
78
79
# File 'lib/bade/ruby_extensions/string.rb', line 77

def remove_indent!(indent, tabsize)
  remove_first!(__chars_count_for_indent(indent, tabsize))
end

#remove_last(count = 1) ⇒ Object



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

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

#remove_last!(count = 1) ⇒ Object



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

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

#single_quoteString

Creates new string surrounded by single quotes

Returns:



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

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