I don't understand this 5/8 frequencies


#1

https://www.codecademy.com/courses/learn-ruby/lessons/create-a-histogram/exercises/iterating-over-the-array?action=lesson_resume&link_content_target=interstitial_lesson

My code is working, but I have 2 questions


puts "write something"

text = gets.chomp

words = text.split (" ")

frequencies = Hash.new(0) 

words.each do |word|
  

 frequencies[word] +=1
  
end
  1. Why almost everybody in the forum used this solution

words.each { |word| frequencies[word] += 1  }


and mine works as well?

  1. Can anybody explain me the logic behind this string?
    If I understood correctly, for each given word… how the code knows that has to increase the value associated with the key?

words.each { |word| frequencies[word] += 1  }


Thank you


#2

Yours works as well because it is virtually the same model… A block.

Recall that when we defined our hash it was declared with a default starting value for all new keys.

As each new word is encountered (one not yet in the hash), a new key is created and initialized, and then the word is counted and the value for that key bumped by 1.

Any exisiting words just get their respective key values incremented.


#3

I think I got it. It’s still not totally clear but I hope it will get better keep working on it.

Thank you!


#4

Let’s pretend that there is no way to set a default for new keys. That means we have to initialize new keys manually, so they can be incremented. The initial value must be numeric or we cannot do arithmetic with it.

frequencies = {}
words = "the rain in spain falls mainly in the plains in the spring and in the fall".split()
words.each do |word|
    frequencies[word] = 0 unless frequencies[word].is_a? Numeric
    frequencies[word] += 1
end
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
 >
=> ["the", "rain", "in", "spain", "falls", "mainly", "in", "the", "plains", "in", "the", "spring", "and", "in", "the", "fall"]
 > frequencies
=> {"the"=>4, "rain"=>1, "in"=>4, "spain"=>1, "falls"=>1, "mainly"=>1, "plains"=>1, "spring"=>1, "and"=>1, "fall"=>1}   

https://repl.it/K6du


#5

Ok, and I got this. My only concern is:
given this string where word is the key and usually (see lesson 7/16 and 8/16 of DATA STRUCTURES) to assign a value we use one of those two formulas key => value or [“key”] = “value”. How the script knows that +=1 increment the value? Is it just the syntax?

Sorry if I’m kinda slow.


#6

For two sequential operations, yes. += is a compound operator, in this case an augmentation operator. In a generic sense it is an assignment operator.

The arithmetic operator is tackled first, then assigned back to the variable, effectively replacing the old value with the new one.

a += 1  =>  a = a + 1 

In the exercise code, word is a key variable so always contains a key name. We access the value by querying the subscript.

Eg.

object = {
  'a' => 1,
  'b' => 2,
  'c' => 3,
  'x' => 24,
  'y' => 25,
  'z' => 26
}

object.each { |key, value| puts object[key] }
> 
1
2
3
24
25
26
=> {"a"=>1, "b"=>2, "c"=>3, "x"=>24, "y"=>25, "z"=>26}
 > 

#7

Ok, finally I got it!

Thank you!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.