문제

I have a bit of a brain buster i am trying to refactor this method. there are several goals behind doing this, first is so that if anyone reads this code once they can move on because they wouldn't have questions about it.

Second i am hoping there is a faster way of doing this. i mean in my mind its almost like a p vs np thing, but i am sure there are neater ways of accomplishing it. Applying the Solid principles is the goal.

This method receives a string and break it down into individual words. Then it inspects each of the words to find the following suffixes -er, -ers, -ed, -ant, -and and -anned. with each of these suffixes there is a new replacement -er becomes -xor, -ers becomes -xors, -ed becomes -d, -ant, -and and -anned becomes -&(eg. banned becomes b&)

This is what i have so far for the -er and -ers suffixes. its ugly and i bet really slow.

def reconstruct_sentence
  s = @sentence.split(/\W+/)
  s.each_with_index do |word, i|
    if word.end_with?("er")
      s[i] = word.chomp("er") + ("xor")
    elsif word.end_with?("ers")
      s[i] = word.chomp("ers") + ("xors")
    else
      return
    end
    s.join(" ")
  end
end

I think what i am asking for is far out there and refactoring takes i think a few years of code experience to get used to. but as i go along i can see that this method has more then one purpose breaking solid. so i broke it like this.

def edit_sentence
  split_sentence(@sentence) #this would be the sentence that is initialized
  sentence.each_with_index do |word, i|
    if word.end_with?("er")
      sentence[i] = word.chomp("er") + ("xor")
    elsif word.end_with?("ers")
      sentence[i] = word.chomp("ers") + ("xors")
    else
      return
    end
    reconstruct_sentence
  end
end

def split_sentence(sentence)
  sentence.split(/\W+/)
end

def reconstruct_sentence
  sentence.join(" ")
end

the part that i am struggling to re-factor is getting all the suffixes in one method call. i first thought with all the repetition i should use a hash to save all the old suffixes as a key and the new ones as a value but i reckon its going to get complicated like meta programming to the max. any advice? and does anyone know a good book on refactoring patterns?

thanks in advance.

도움이 되었습니까?

해결책

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"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top