Creating A Hangman Game With Ruby

4 min read

Week three of the internship at 8th Light is complete, and what a tough week it was. This week’s challenge was difficult.

Combining it with a lack of self care, along with imposter syndrome rearing its ugly head, was a recipe for an unpleasant week.

The challenge - Hangman

This week’s challenge was to build a game of Hangman that you can play on the command line. It needed to meet specific requirements.

Monday started off with a spike, to see if we could hack bits of the game on our own without tests. I managed to create a working Hangman game. Ignore the absence of refactoring. It works, ok? 🎉

Clink clink!

The next challenge was to scrap the spike, and pair with a fellow intern to create a hangman game with classes and tests.

Derek and I started on Monday afternoon, writing down the different classes and their behaviours on the biggest Post-It notes we’ve seen.

Our aim was to extract key elements and think of classes and objects as nouns, and their behaviours as methods. We read about this in Learn Ruby The Hard Way.

On Tuesday morning, the team had code along session with Andrew — one of the mentors for our cohort. We started a Hangman game from scratch, beginning with a Word class and creating a couple of methods for that class.

The code along was extremely helpful. As the session continued, it became apparent that although Derek and I were headed in the right direction, we took a wrong turn. We decided to scrap our code and start from scratch.

We started with a Word class, created tests, added code to pass the tests and repeated with other classes. By Thursday, all of our tests had passed and we were ready to piece the game together.

Putting it together

I thought this process would be quite straightforward. How naive of me! The first time we managed to get our game running, it was stuck in an infinite loop and wasn’t outputting what we expected to the command line.

Thursday consisted of a tweak here, and a tweak there, along with adding gets, puts and print in our game. Eventually the infinite loop was fixed, and the game was outputting what we expected. It worked! 🎉🙌🏽

Oh, but we celebrated way too soon. Why? Well, replacing returns with puts and print caused our tests to fail. With Andrew’s advice, we started to look into doubles, mocks and stubs in RSpec. Maybe I was too tired at this point, because nothing about it made sense. Zero. Zilch. Nada.

Self care

By Thursday evening, I felt slightly defeated and drained. I had difficulty switching off, and Hangman even made a guest appearance in one of my dreams. When Thursday evening rolled around, I felt like I had been awake for 48+ hours.

I planned to read more into doubles, mocks and stubs in the evening, but upon arriving home I decided against that. I needed a break, and I managed to switch off completely. By Friday morning, I felt quite refreshed and ready to tackle our failing tests.

A breakthrough

At stand up on Friday, I expressed I was experiencing issues with RSpec and testing output. Devlin pointed us in the right direction, starting with this post on Slack Overflow. Together, we tried it with one of the failing tests, and after a little bit of tweaking, it passed!

1it "prints rules to user" do
2 display =
3 expect {display.game_rules}.to output(%Q(
4 Welcome to Hangman!
5 To win, you need to guess the mystery word or you die.
6 You can have up to 8 incorrect guesses.
7 Let's begin!\n )).to_stdout

After getting over this hump, and the remaining failing tests were tweaked, the grey cloud lifted and all was well again in Elle’s world. You can find the completed version of the Hangman game here.

In conclusion, this week probably felt harder than it should have, due to feeling exhausted. However, I’m extremely grateful to be surrounded by a team of crafters, mentors and interns, who are full of knowledge and always willing to help.

After completing the week’s challenge, I’m back on cloud 9 with a sense of accomplishment, along with excitement and gratitude for this opportunity at 8th Light.

Things I've learned

  • Testing output with RSpec
  • Using branches in Git
  • bin/console and permissions to execute the file

Things I've struggled with

  • Testing output with RSpec
  • Scope of instance variables
  • Knowing when to switch off and take breaks

What I want to focus on

Next post:
What Is Dependency Injection?