Module: StringHelpers
- Defined in:
- lib/string_helpers.rb
Class Method Summary collapse
-
.corresponding_index(strings, mark, start_index) ⇒ Object
Return index of string in list that contains given mark.
- .equalize(stringA, stringB) ⇒ Object
- .equalize_capitalization(stringA, stringB) ⇒ Object
-
.equalize_interpunction(stringA, stringB) ⇒ Object
Return stringA with spacing around its interpunction equal to that in StringB.
-
.equalize_surrounding_space(stringA, stringB) ⇒ Object
Return second string, with outer surrounding spacing like first string.
Class Method Details
.corresponding_index(strings, mark, start_index) ⇒ Object
Return index of string in list that contains given mark. Starting search at start_index. Return nil if search fails.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/string_helpers.rb', line 70 def self.corresponding_index( strings, mark, start_index ) regexp = /#{Regexp.quote( mark )}/ tail = strings[ start_index .. -1 ] return nil if tail == nil tail_index = tail.index { |item| item.match( regexp ) } return nil if tail_index == nil start_index + tail_index end |
.equalize(stringA, stringB) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/string_helpers.rb', line 3 def self.equalize stringA, stringB step1 = equalize_interpunction( stringA, stringB ) step2 = equalize_surrounding_space( step1, stringB ) step3 = equalize_capitalization( step2, stringB ) end |
.equalize_capitalization(stringA, stringB) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/string_helpers.rb', line 12 def self.equalize_capitalization stringA, stringB if stringB.match( /^\s*[A-Z]/ ) and stringA.match( /^\s*[a-z]/ ) part1 = stringA.gsub( /^(\s*)(.*)$/, '\1' ) part2 = stringA.gsub( /^(\s*)(.*)$/, '\2' ) return part1 + part2.capitalize else return stringA end end |
.equalize_interpunction(stringA, stringB) ⇒ Object
Return stringA with spacing around its interpunction equal to that in StringB
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/string_helpers.rb', line 43 def self.equalize_interpunction stringA, stringB punctuation_marks = "!?.,:;'@|$%^&*-+={()}\/\\\\\#\"\\\[\\\]" space_mark_space = /(\s*[#{punctuation_marks}]\s*)/ get_mark = /\s*([#{punctuation_marks}])\s*/ splitA = stringA.split( space_mark_space ) # "'a' ( bla ) (c )" => ["", "'", "a", "' ", "", "( ", "bla )", " (", "c )"] splitB = stringB.split( space_mark_space ) splitB.each_with_index do |b,indexB| next unless b.match( space_mark_space ) markB = b.gsub( get_mark, '\1' ); indexA = corresponding_index( splitA, markB, indexB ) splitA[ indexA ] = b unless indexA == nil end splitA.join('') end |
.equalize_surrounding_space(stringA, stringB) ⇒ Object
Return second string, with outer surrounding spacing like first string
29 30 31 32 33 34 35 36 |
# File 'lib/string_helpers.rb', line 29 def self.equalize_surrounding_space stringA, stringB prefix = stringB.match( /^\s*/ )[0] suffix = stringB.match( /\s*$/ )[0] middle = stringA.gsub( /^\s*|\s*$/, '' ) prefix + middle + suffix end |