First, your code doesn't work properly, since the return
keyword returns from the whole method, instead of continuing.
For a short version of what you try to do, you can use gsub
with replacements:
def reconstruct_sentence
@sentence.gsub(/ers?\b/, 'er' => 'xor', 'ers' => 'xors')
end
@sentence = 'this is a tester without any eaters'
reconstruct_sentence
# => "this is a testxor without any eatxors"
More generically you can do:
def reconstruct_sentence
replacements = {'er' => 'xor', 'ers' => 'xors', 'ed' => 'd',
'ant' => '&', 'and' => '&', 'anned' => '&'}
@sentence.gsub(Regexp.union(replacements.keys), replacements)
end
@sentence = 'this is a tester without any planned fighters'
reconstruct_sentence
# => "this is a testxor without any pl& fightxors"