Hi, i dont understand


Continuing the discussion from Confuse on these code:

Replace this line with your code.


The above line declares a new hash named frequencies. When new keys are defined, their initial value will be 0, automatically set in the background by Ruby. This will permit math on the values for every new key.

The above line constructs the hash from the inputted text. For example, let's say the input is,

so sorry to be so sorry so much

The resulting hash will look something like this (in any order):

{ 'so' => 3, 'sorry' => 2, 'to' => 1, 'be' => 1, 'much' => 1 }

The above line is pretty busy. Ruby creates an array of tuples from the hash and sorts them by value from lowest to highest. The resulting array from the above will look something like this...

[('to', 1), ('be', 1), ('much', 1), ('sorry', 2), ('so', 3)]

This is what gets assigned to frequencies, which means we overwrite the original hash and re-purpose the variable. This is also known as re-use.

The final line flips the order so the array will be ordered from highest value to lowest.


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