# Scrabble Stems Ruby Quiz question

**URL:** <https://www.ruby-forum.com/t/scrabble-stems-ruby-quiz-question/121866>\
**Category:** Ruby\
**Created:** [December 2, 2007, 3:51am UTC](https://www.ruby-forum.com/t/scrabble-stems-ruby-quiz-question/121866 "2007-12-02T03:51:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marksbrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@marksbrain](https://www.ruby-forum.com/u/marksbrain)\
**Post date:** [December 2, 2007, 3:51am UTC](https://www.ruby-forum.com/t/scrabble-stems-ruby-quiz-question/121866/1 "2007-12-02T03:51:02Z")

</div>

Hi all,

could someone explain the line:  
(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|  
f.each do |word|  
word.chomp!  
next if word.length != 7  
word.downcase!  
letters = word.split(//).sort!  
uniques = letters.uniq  
word = letters.join  
uniques.each do |letter|  
stem = word.sub(letter, “”)  
(STEMS[stem] ||= {})[letter] = 1  
end  
end  
end

result = STEMS.delete\_if { |k,v| v.size \< CUTOFF }.  
sort\_by { |k,v| v.size }.  
reverse!.  
collect! { |k,v| [k, v.size] }

## result.each do |stem, combining| puts “#{stem} #{combining}” end

cheers,

---

<div class="post-metadata">

**Author:** ![marksbrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@marksbrain](https://www.ruby-forum.com/u/marksbrain)\
**Post date:** [December 2, 2007, 5:10am UTC](https://www.ruby-forum.com/t/scrabble-stems-ruby-quiz-question/121866/2 "2007-12-02T05:10:30Z")

</div>

On Dec 1, 8:48 pm, Mark W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

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.

---

<div class="post-metadata">

**Author:** ![marksbrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@marksbrain](https://www.ruby-forum.com/u/marksbrain)\
**Post date:** [December 2, 2007, 5:40am UTC](https://www.ruby-forum.com/t/scrabble-stems-ruby-quiz-question/121866/3 "2007-12-02T05:40:52Z")

</div>

Hi Yermej,

On Sat, 1 Dec 2007 20:05:45 -0800 (PST)  
yermej [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 😉

> 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,
