# Scrabble Stems Ruby Quiz question

**URL:** <https://rubytalk.org/t/scrabble-stems-ruby-quiz-question/42667>\
**Category:** ruby-talk\
**Created:** [2 December 2007 02:50 UTC](https://rubytalk.org/t/scrabble-stems-ruby-quiz-question/42667 "2007-12-02T02:50:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark9](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark9](https://rubytalk.org/u/Mark9)\
**Post date:** [2 December 2007 02:50 UTC](https://rubytalk.org/t/scrabble-stems-ruby-quiz-question/42667/1 "2007-12-02T02:50:04Z")

</div>

Hi all,

could someone explain the line:  
&nbsp;&nbsp;&nbsp;&nbsp;(STEMS[stem] ||= {})[letter] = 1  
in the code below?  
This is a solution (Carlos) from the Scrabble Stems Ruby quiz.

> **···**
>
> ---------------------------------------------------------------  
> DICT = "/usr/share/dict/words"  
> CUTOFF = ARGV[0].to\_i
> 
> STEMS = {}
> 
> File.open(DICT) do |f|  
> &nbsp;&nbsp;f.each do |word|  
> &nbsp;&nbsp;&nbsp;&nbsp;word.chomp!  
> &nbsp;&nbsp;&nbsp;&nbsp;next if word.length != 7  
> &nbsp;&nbsp;&nbsp;&nbsp;word.downcase!  
> &nbsp;&nbsp;&nbsp;&nbsp;letters = word.split(//).sort!  
> &nbsp;&nbsp;&nbsp;&nbsp;uniques = letters.uniq  
> &nbsp;&nbsp;&nbsp;&nbsp;word = letters.join  
> &nbsp;&nbsp;&nbsp;&nbsp;uniques.each do |letter|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stem = word.sub(letter, "")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(STEMS[stem] ||= {})[letter] = 1  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> result = STEMS.delete\_if { |k,v| v.size \< CUTOFF }.  
> &nbsp;&nbsp;sort\_by { |k,v| v.size }.  
> &nbsp;&nbsp;reverse!.  
> &nbsp;&nbsp;collect! { |k,v| [k, v.size] }
> 
> result.each do |stem, combining| puts "#{stem} #{combining}" end  
> ---------------------------------------------------------------
> 
> cheers,
> 
> --  
> Mark

---

<div class="post-metadata">

**Author:** ![yermej](https://avatars.discourse-cdn.com/v4/letter/y/b2d939/32.png) [@yermej](https://rubytalk.org/u/yermej)\
**Post date:** [2 December 2007 04:09 UTC](https://rubytalk.org/t/scrabble-stems-ruby-quiz-question/42667/2 "2007-12-02T04:09:59Z")

</div>

It's equivalent to:

STEMS[stem] = STEMS[stem] || {}  
STEMS[stem][letter] = 1

Where that first line means: if STEMS[stem] isn't defined, assign a  
new Hash to it.

It could also be done like this:

STEMS = Hash.new {|h, k| h[k] = {}} # near the top of your example  
code

STEMS[stem][letter] = 1 # and this in place of the line you asked  
about

Hash.new allows you to use a block to define the behavior of the hash  
if the element isn't found. With no block (as in the example code you  
posted), nil is returned.

> **···**
>
> On Dec 1, 8:48 pm, Mark Woodward \<markonli...@internode.on.net\> wrote:
> 
> > Hi all,
> > 
> > could someone explain the line:  
> > &nbsp;&nbsp;&nbsp;&nbsp;(STEMS[stem] ||= {})[letter] = 1  
> > in the code below?

---

<div class="post-metadata">

**Author:** ![Mark9](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark9](https://rubytalk.org/u/Mark9)\
**Post date:** [2 December 2007 04:39 UTC](https://rubytalk.org/t/scrabble-stems-ruby-quiz-question/42667/3 "2007-12-02T04:39:59Z")

</div>

Hi Yermej,

> \> Hi all,  
> \>  
> \> could someone explain the line:  
> \> (STEMS[stem] ||= {})[letter] = 1  
> \> in the code below?
> 
> It's equivalent to:
> 
> STEMS[stem] = STEMS[stem] || {}  
> STEMS[stem][letter] = 1

The '[letter] = 1' appended to the end was the bit I didn't understand.

Definitely easier to read as your e.g. above. But I guess once you know  
the syntax (STEMS[stem] ||= {})[letter] = 1 makes sense. A bit too  
'Perlish' for a newbie though 😉

> Where that first line means: if STEMS[stem] isn't defined, assign a  
> new Hash to it.
> 
> It could also be done like this:
> 
> STEMS = Hash.new {|h, k| h[k] = {}} # near the top of your example  
> code
> 
> STEMS[stem][letter] = 1 # and this in place of the line you asked  
> about
> 
> Hash.new allows you to use a block to define the behavior of the hash  
> if the element isn't found. With no block (as in the example code you  
> posted), nil is returned.

thanks,

> **···**
>
> On Sat, 1 Dec 2007 20:05:45 -0800 (PST) yermej \<yermej@gmail.com\> wrote:
> 
> > On Dec 1, 8:48 pm, Mark Woodward \<markonli...@internode.on.net\> wrote:
> 
> --  
> Mark
