# Why is this variable here?

**URL:** <https://www.ruby-forum.com/t/why-is-this-variable-here/259522>\
**Category:** Uncategorized\
**Created:** [May 9, 2020, 12:20am UTC](https://www.ruby-forum.com/t/why-is-this-variable-here/259522 "2020-05-09T00:20:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaulGureghian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba8739/32.png) [@PaulGureghian](https://www.ruby-forum.com/u/PaulGureghian)\
**Post date:** [May 9, 2020, 12:20am UTC](https://www.ruby-forum.com/t/why-is-this-variable-here/259522/1 "2020-05-09T00:20:17Z")

</div>

Why is this variable here?  
And should I put ‘word’ there as well ? Rubocop wants me to.

 ![Histogram](https://www.ruby-forum.com/uploads/default/original/2X/a/a5f43b7b6dfea5f1841e4f8a689a6530c298fcc4.png)

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 9, 2020, 1:12am UTC](https://www.ruby-forum.com/t/why-is-this-variable-here/259522/2 "2020-05-09T01:12:11Z")

</div>

(You can post code directly, in between ``` back ticks - images are harder to read)

See: [module Enumerable - RDoc Documentation](https://docs.ruby-lang.org/en/2.7.0/Enumerable.html#method-i-sort_by)

sort\_by returns the value on which to sort the list.

Your `frequencies` are a hash, and so a collection of key-\>value pairs, in your case word-\>count pairs. The sort\_by call is returning an array of [word, count] pairs, ordered by count.

```auto
2.7.1 :001 > freq = {"a" => 2, "b" => 3, "c" => 1}
2.7.1 :002 > freq.sort_by {|word, count| count}
 => [["c", 1], ["a", 2], ["b", 3]] 
2.7.1 :003 > freq.sort_by {|word, count| word}
 => [["a", 2], ["b", 3], ["c", 1]] 
2.7.1 :004 > 

```

Notice how line 2 returns the word-count pairs ordered by count, and line 3 returns the word-count pairs ordered by word.

Rubocop may be complaining because you have a named variable, `word`, which does not get used. You could try replacing ‘word’ with ‘\_’.

---

<div class="post-metadata">

**Author:** ![PaulGureghian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba8739/32.png) [@PaulGureghian](https://www.ruby-forum.com/u/PaulGureghian)\
**Post date:** [May 9, 2020, 2:20am UTC](https://www.ruby-forum.com/t/why-is-this-variable-here/259522/3 "2020-05-09T02:20:31Z")

</div>

So, in my code it’s ordered by count ?  
‘count’ should be on the same line as the .sortby so its clear what it is doing.
