Class: String

Inherits:
Object show all
Defined in:
lib/diakonos/core-ext/string.rb

Instance Method Summary collapse

Instance Method Details

#expand_tabs(tab_size) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/diakonos/core-ext/string.rb', line 12

def expand_tabs(tab_size)
  s = dup

  while s.sub!( /\t/ ) {
    match = Regexp.last_match
    index = match.begin( 0 )

    " " * ( tab_size - (index % tab_size) )
  }
  end

  s
end

#group_index(regexp, offset = 0) ⇒ Object

Works like normal String#index except returns the index of the first matching regexp group if one or more groups are specified in the regexp. Both the index and the matched text are returned.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/diakonos/core-ext/string.rb', line 44

def group_index( regexp, offset = 0 )
  if regexp.class != Regexp
    return index( regexp, offset )
  end

  i = nil
  match_text = nil
  working_offset = 0
  loop do
    index( regexp, working_offset )
    match = Regexp.last_match
    if match
      i = match.begin( 0 )
      match_text = match[ 0 ]
      if match.length > 1
        # Find first matching group
        1.upto( match.length - 1 ) do |match_item_index|
          if match[ match_item_index ]
            i = match.begin( match_item_index )
            match_text = match[ match_item_index ]
            break
          end
        end
      end

      break if i >= offset
    else
      i = nil
      break
    end
    working_offset += 1
  end

  [ i, match_text ]
end

#group_rindex(regexp, offset = length) ⇒ Object

Works like normal String#rindex except returns the index of the first matching regexp group if one or more groups are specified in the regexp. Both the index and the matched text are returned.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/diakonos/core-ext/string.rb', line 83

def group_rindex( regexp, offset = length )
  if regexp.class != Regexp
    return rindex( regexp, offset )
  end

  i = nil
  match_text = nil
  working_offset = length
  loop do
    rindex( regexp, working_offset )
    match = Regexp.last_match
    if match
      i = match.begin( 0 )
      match_text = match[ 0 ]
      if match.length > 1
        # Find first matching group
        1.upto( match.length - 1 ) do |match_item_index|
          if match[ match_item_index ]
            i = match.begin( match_item_index )
            match_text = match[ match_item_index ]
            break
          end
        end
      end

      break if i <= offset
    else
      i = nil
      break
    end
    working_offset -= 1
  end

  [ i, match_text ]
end

#newline_splitObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/diakonos/core-ext/string.rb', line 26

def newline_split
  retval = split( /\\n/ )
  if self =~ /\\n$/
    retval << ""
  end
  if retval.length > 1
    retval[ 0 ] << "$"
    retval[ 1..-2 ].collect do |el|
      "^" << el << "$"
    end
    retval[ -1 ] = "^" << retval[ -1 ]
  end
  retval
end

#to_bObject



3
4
5
6
7
8
9
10
# File 'lib/diakonos/core-ext/string.rb', line 3

def to_b
  case downcase
  when "true", "t", "1", "yes", "y", "on", "+"
    true
  else
    false
  end
end