Reputation: 1869
I'm looking for a way to search for all occurrences of a pattern in a string, perform some nontrivial manipulation of the string returned, and then replace it.
Ruby's String#gsub! method isn't a good fit because I need to take the returned occurrence and then manipulate it to come up with the value that will be used to replace it.
Ruby's String#scan method, of course, can be used to find all occurrences of the pattern but I'm not sure how to replace the returned occurrences once I've made the necessary changes to them.
The code example below is not what I'm actually working on demonstrates the sort of thing I'm attempting to accomplish.
# String
string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
# Look for all words that start with a vowel
string.scan(/\s[aeiou]\w{1,}/i).each do |word|
# Manipulate the words that match the pattern
word.upcase
# Replace the word in the string with the manipulated value
# Need some help here
end
# Print the modified string
puts string
Upvotes: 3
Views: 4468
Reputation: 110725
You don't have to use gsub
:
string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
string.split.map {|word| "aeiou".include?(word[0]) ? word.capitalize : word}.join(' ')
# => "Lorem Ipsum dolor sit Amet, consectetur Adipisicing Elit."
Upvotes: 0
Reputation: 24478
String#gsub!
can be used with a block, which is passed the matched string. The block's result is then used to replace the match:
# String
string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
string.gsub!(/\s[aeiou]\w{1,}/i) { |m| m.upcase }
Upvotes: 3
Reputation: 5563
You can use gsub
with a block, so
string.gsub(/\s[aeiou]\w{1,}/i) do |word|
word.upcase
end
Upvotes: 10
Reputation: 53535
Did you mean something like that:
string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
copy = string
# Look for all words that start with a vowel
string.scan(/\s[aeiou]\w{1,}/i).each do |word|
# Manipulate the words that match the pattern
copy.gsub! word, word.upcase
end
# Print the modified string
puts copy
Upvotes: 2