Avatar of davearonson

davearonson's solution

to Hello World in the Ruby Track

Published at Jul 13 2018 · 2 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

The classical introductory exercise. Just say "Hello, World!".

"Hello, World!" is the traditional first program for beginning programming in a new language or environment.

The objectives are simple:

  • Write a function that returns the string "Hello, World!".
  • Run the test suite and make sure that it succeeds.
  • Submit your solution and check it at the website.

If everything goes well, you will be ready to fetch your first real exercise.

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby hello_world_test.rb

To include color from the command line:

ruby -r minitest/pride hello_world_test.rb


This is an exercise to introduce users to using Exercism http://en.wikipedia.org/wiki/%22Hello,_world!%22_program

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


  gem 'minitest', '>= 5.0.0'
  require 'minitest/autorun'
  require_relative 'hello_world'
rescue Gem::LoadError => e
  puts "\nMissing Dependency:\n#{e.backtrace.first} #{e.message}"
  puts 'Minitest 5.0 gem must be installed for the Ruby track.'
rescue LoadError => e
  puts "\nError:\n#{e.backtrace.first} #{e.message}"
  puts DATA.read
  exit 1

# Common test data version: 1.0.0 4b9ae53
class HelloWorldTest < Minitest::Test
  def test_say_hi
    # skip
    assert_equal "Hello, World!", HelloWorld.hello


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.

For more guidance as you work on this exercise, see
class HelloWorld
  def self.hello(name="World")
    "Hello, #{name}!"

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of davearonson


The main one was whether to directly default the name, or default it to nil or blank and check whether it was "present". That would have been OK except for the last test, which demanded that an empty string still be treated as being there, i.e., having it say "Hello, !", rather than "Hello, World!" or just "Hello!", which I think would make more sense.

Another single vs double quotes. I'm starting to lean towards the idea that the tiny theoretical usually-not-really-seen performance gains from using single quotes, are not worth the inconvenience of retrofitting double-quotes when one needs to embed a single-quote or an interpolated variable. That approach was in fact supported when, to make the second test pass, I had to change the constant World inside the returned string, to an interpolated variable.

Avatar of bjmllr

I agree about double quotes. I like to add this to .rubocop.yml: Style/StringLiterals: EnforcedStyle: double_quotes

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?