January 7, 2018

Practicing Ruby Through Exercism!

I think I have covered Ruby fundamentals enough this past week! I covered:

Now, to put all that knowledge that I think I have to the test! I could start with CodeWars or HackerRank...

Instead, I wanted to check out the site Franklin Webber mentioned in his webinar, Exercism.io, an open-source practice area written by a former JavaRanch.com volunteer. 

What is Exercism?

"Exercism.io is free and Open Source. The website prototype is written in Ruby using the Sinatra web framework. It is licensed under the GNU Affero General Public License. The command-line client is written in Go, and distributed under the MIT license.

"The exercises in the various languages are also released under the MIT license. An overwhelmingly large number of people have contributed to the codebase.

"An even larger number of people have contributed through participation; by completing exercises and participating in the discussions about code.

"Creator: Katrina Owen. Katrina is a polyglot developer and Ruby Hero award winner who accidentally became a developer while pursuing a degree in molecular biology. She began nitpicking code back in 2006 while volunteering at JavaRanch, and got hooked. When programming, her focus is on automation, workflow optimization, and refactoring. She cares deeply about open source and contributes to several projects outside of exercism".

Get A GitHub Account

I was able to sign up with my GitHub account. Don't have one? Get one! Not only is it a great way to show code samples to future employers, you can also demo your code to other members of Exercism through GitHub.

How Does It Work? 

From Exercism.io / How It Works / Newbie:

"Using Exercism requires three tools:
  • "Your text editor: Write a solution to an exercise using your favorite text editor.
  • "Your command line interface: Fetch problems and submit solutions via the command line (or terminal).
  • "The exercism.io website: Review feedback on your solution and discuss it with other learners on the website.
"For each exercise that you do, you'll go through the same basic steps.
  1. "Fetch the exercise using the command line.
  2. "Write code to solve the exercise on your own computer, satisfying each of the tests.
  3. "Submit your solution using the command line. If you get stuck, submit what you have and ask for help. You also get to see other people's solutions to the same problem, which could help you figure it out.
  4. "Review feedback and look at how other people solved the same problem on the website. Ask questions about what seems interesting or confusing!
  5. "Improve your solution and resubmit as many times as desired".

Getting the Command Line Client

Since I have a Windows 10 Machine at home, the first thing I did was go to the GitHub site of Exercism / Windows Installer at https://github.com/exercism/windows-installer/releases and install it.

If you are already logged into the site, you can copy and paste the code to configure Exercism with your API Key, from http://exercism.io/how-it-works/newbie

After installation, you can see if it works by typing in the language you want to practice, such as in http://exercism.io/languages/ruby/about:

cd C:\Exercism
exercism fetch ruby

... And that starts you with the first problem: Hello World. 

Open the Problem in a Text Editor

You can use the new Emacs 25.3, the new Vim 7, the brand new Notepad++ 7.5.4, Sublime Text 3, RubyMine 2017.3, or any other way you can to input code.

Lately, though, I have been experimenting with Atom.io, a tool I heard about when I was tinkering with JavaScript last year.

Going into C:\Exercism\ruby\hello-world I found three files:

  • GETTING_STARTED.md, a Markdown file
  • hello_world_test.rb, a Ruby file
  • README.md

After opening them all, I found:

  • README tells you how to install a Ruby library (a "Ruby Gem") called "minitest".
  • GETTING_STARTED.md talks about how these exercises will touch upon Test-Driven Development (TDD). You can get started with a Ruby TDD Tutorial from JumpStart Labs using minitest. It will also walk you through how to create a Hello World ruby file.
  • hello_world_test.rb presents a working minitest file. 
After running the test as instructed on my MS-DOS Command Prompt, hello_world_test.rb, it failed, as the instructions told me it would.
  • C:\Exercism\ruby\hello-world>ruby hello_world_test.rb
Yes, Ruby will run on a Windows 10 machine. You just need to install it.

Create a Blank Hello World File

Why did it fail? Because hello_world.rb does not yet exist! I had not created it. 

"You got an error, which is exactly as it should be.
"This is the first step in the Test-Driven Development
(TDD) process.

"The most important part of the error is

   cannot load such file

"It's looking for a file named hello_world.rb that doesn't
exist yet.

"To fix the error, create an empty file named hello_world.rb
in the same directory as the hello_world_test.rb file.

"Then run the test again".

Add Content To The File

Run the test again, and it is another new error. Why? The hello_world.rb file is blank.

Next, you need to start adding to the blank hello_world file.

You will keep on doing this:

  • Working towards a solution to a new exercise.
  • Running your program. 
  • Finding errors. 
  • Reading the error messages, and figuring out how to interpret them.
  • Fixing errors. 
  • Running your program.
  • Success! Jump to the next exercise.

And the test that needs to pass? The MiniTest HelloWorldTest:

class HelloWorldTest < Minitest::Test
  def test_say_hi
    # skip
    assert_equal "Hello, World!", HelloWorld.hello

This is a pretty wonderful simulation of how programming in the real world really works!

My advice? Never cut-and-paste code. Even if you are using someone else's solution, always, always type it out. Run it. See if you can understand it. Compare the keywords used to the official Ruby documentation.

You only learn by grappling with a problem and studying a solution. Cutting-and-pasting will never do you any favors in learning how to code.

Pssst! They don't want you to do "puts 'Hello, World!'" and print the output to the screen. They want you to:

  • return 'Hello, World!'
... If you are brand new to Ruby, you just might not understand that is what MiniTest wants.

Does it Pass? Ship it! 

"When everything is passing, you can submit your code with the following

    $ exercism submit hello_world.rb

... And there were minor complications:

  • Exercism is on C:\, not C:\Exercism\ruby\hello=world directory. It is not a global command (yet)
  • exercism fetch: shows where exactly where hello-world is located, i.e. C:\Exercism\ruby\hello-world, so I had to do the following...
  • C:\Exercism>exercism submit C:\Exercism\ruby\hello-world\hello_world.rb 

"Programmers generally spend far more time reading code than writing it.

"To benefit the most from this exercise, find 3 or more submissions that you can learn something from, have questions about, or have suggestions for.

"Post your thoughts and questions in the comments, and start a discussion.
Consider revising your solution to incorporate what you learn.

"Yours and others' solutions to this problem: http://exercism.io/tracks/ruby/exercises/hello-world"

One problem down. Eighty-nine to go! Here are all the exercises!

This looks like it is going to be fun!

  • exercism fetch ruby

Happy Testing!

-T.J. Maher
Twitter | LinkedIn | GitHub

// Sr. QA Engineer, Software Engineer in Test, Software Tester since 1996.
// Contributing Writer for TechBeacon.
// "Looking to move away from manual QA? Follow Adventures in Automation on Facebook!"
Post a Comment