Class: String
Instance Method Summary collapse
- #expand_tabs(tab_size) ⇒ Object
-
#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.
-
#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.
- #newline_split ⇒ Object
- #to_b ⇒ Object
Instance Method Details
#expand_tabs(tab_size) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/diakonos/core-ext/string.rb', line 12 def ( tab_size ) s = dup while s.sub!( /\t/ ) { |match_text| 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.
41 42 43 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 |
# File 'lib/diakonos/core-ext/string.rb', line 41 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.
80 81 82 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 118 119 |
# File 'lib/diakonos/core-ext/string.rb', line 80 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.end( 0 ) - 1 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.end( match_item_index ) - 1 match_text = match[ match_item_index ] break end end end if match_text == "" # Assume that an empty string means that it matched $ i += 1 end break if i <= offset else i = nil break end working_offset -= 1 end [ i, match_text ] end |
#newline_split ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/diakonos/core-ext/string.rb', line 23 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_b ⇒ Object
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 |