I tried to do some refactors to change each block in an injection, but it does not work and I do not Why do you understand
Here is the code that works before refactoring:
class string # Create a word profile for the given word word profile # 26 is an array of integers - each The number of the integer word is the number of the number that appears in each letter # word #def profile profile = array.New (26) {0} self.Downcase.plit (//). Do each. Letters | # Only describes A-Z profile [letters.ord - 'a'.ord] + = 1 unless letter.ord & gt; 'Z'.ord end profile end end
And here is my reflection that does not work:
class string # . The term profile # 26 is an array of integers - each integer is the number of number of words that each letter # appears in the word #def profile itself.DownCas.plit (//). Injection (Arrays.New (26) {0}). Profile, letter | # Only describes A-Z profile [letters.ord - 'a'.ord] + = 1 unless letter.ord & gt; 'Z' End and end When I try and execute the refacted method, I get
'block in profile': For the undefined method `[] = '1: FixName (noMthyearEnter)
If I think that is correct, then it does not like the array reference operator on the profile object in my restored version , Which means that the start of starting to inject Rta is not working. Is this understanding correct? And if so, why not?
Thank you!
[] =
method assigns assigned value, so move on next The value of profile
will be 1 (because it is the value of the last trip). You want to get the behavior, you have to:
self.downcase.split (//). Inject (Array.new (26) {0}). Profile, letter | # Only describes A-Z profile [letters.ord - 'a'.ord] + = 1 unless letter.ord & gt; 'Z'.ord Profile End
or
self.downcase.split (//). Do injection (Array.new (26) {0}). Profile, letter | Let us write a letter to # letter-a-z profile.tap {profile [letter.ord - 'a'.ord] + = 1 until letter.ord & gt; 'Z'.ord} closing
Comments
Post a Comment