Skip to content

Conversation

@davidmyersdev
Copy link

No description provided.

list[word] += 1
else
list[word] = 1
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can definitely get this done in a single line. Look into other ways of using .each.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I'm guessing ruby supports assignment if a key doesn't exist? E.g.

words.each do |word|
  list[word] += 1
end

Also, I'm not sure what you mean about looking into other ways of using .each. Any suggestions on what to look for?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the Ruby documentation for the Each. Should be a big hint as to how you can get that down to a single line. Your eg code is pretty close, but you can totally get it down to just one line. 😃

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. You mean by using a single line block?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants