From d61adb7f9bf32d909792b4223d3eb05026f00297 Mon Sep 17 00:00:00 2001 From: James Walker Date: Sun, 16 Sep 2018 15:03:34 -0400 Subject: [PATCH] ruby - hello world --- ruby/hello-world/GETTING_STARTED.md | 128 +++++++++++++++++++++++++++ ruby/hello-world/README.md | 45 ++++++++++ ruby/hello-world/hello_world.rb | 5 ++ ruby/hello-world/hello_world_test.rb | 43 +++++++++ 4 files changed, 221 insertions(+) create mode 100644 ruby/hello-world/GETTING_STARTED.md create mode 100644 ruby/hello-world/README.md create mode 100644 ruby/hello-world/hello_world.rb create mode 100644 ruby/hello-world/hello_world_test.rb diff --git a/ruby/hello-world/GETTING_STARTED.md b/ruby/hello-world/GETTING_STARTED.md new file mode 100644 index 0000000..37e2b8a --- /dev/null +++ b/ruby/hello-world/GETTING_STARTED.md @@ -0,0 +1,128 @@ +# Getting Started + +These exercises lean on Test-Driven Development (TDD), but they're not an +exact match. If you want a gentle introduction to TDD using minitest in +Ruby, see the "Intro to TDD" over at JumpstartLab: +http://tutorials.jumpstartlab.com/topics/testing/intro-to-tdd.html + +The following steps assume that you are in the same directory as the test +suite. + +You must have the `minitest` gem installed: + + $ gem install minitest + +## Step 1 + +Run the test suite. It's written using the Minitest framework, and can be +run with ruby: + + $ ruby hello_world_test.rb + +This will fail, complaining that there is no file called `hello_world`. + +To fix the error create an empty file called `hello_world.rb` in the same +directory as the `hello_world_test.rb` file. + +## Step 2 + +Run the test again. It will give you a new error, since now the file exists, +but is empty and does not contain the expected code. + +Depending on what platform you are on, the error will look different, but +the way to fix it will be the same. + +On Windows, it will complain about: + + syntax error, unexpected end-of-input, expecting '(' + +On OS X and Linux, the error will be something like: + + + # Running: + + E + + Finished in 0.001328s, 753.0121 runs/s, 0.0000 assertions/s. + + 1) Error: + HelloWorldTest#test_say_hi: + NameError: uninitialized constant HelloWorldTest::HelloWorld + Did you mean? HelloWorldTest + hello_world_test.rb:19:in `test_say_hi' + + 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips + + +Within the first test, we are referencing a constant named `HelloWorld` when +we say `HelloWorld.hello`. When Ruby sees a capitalized name like +`HelloWorld`, it looks it up in a big huge list of all the constants it knows about, +to see what it points to. It could point to anything, and often in Ruby we have +constants that point to definitions of classes or modules. + +When it looks `HelloWorld` up in its list, it doesn't find anything, so we need +to make one. + +### Fixing the Error + +To fix it, open up the hello_world.rb file and add the following code: + + class HelloWorld + end + +## Step 3 + +Run the test again. + + 1) Error: + HelloWorldTest#test_no_name: + NoMethodError: undefined method `hello' for HelloWorld:Class + hello_world_test.rb:20:in `test_no_name' + +This time we have a `HelloWorld`, but we're trying tell it to `hello`, and +`HelloWorld` doesn't understand that message. + +Open up hello_world.rb and add a method definition inside the class: + + class HelloWorld + def self.hello + end + end + +## Step 4 + +Run the test again. + + 1) Failure: + HelloWorldTest#test_no_name [hello_world_test.rb:11]: + When given no name, we should greet the world. + Expected: "Hello, World!" + Actual: nil + +Up until now we've been getting errors, this time we get a failure. + +An error means that Ruby cannot even run properly because of things like missing +files or syntax errors, or referring to things that don't exist. + +A failure is different. A failure is when Ruby is running just fine +and the test is expecting one outcome, but getting another. + +The test is expecting the `hello` method to return the string `"Hello, World!"`. The easiest way +to make it pass, is to simply stick the string `"Hello, World!"` inside the method definition. + +## Step 5 + +Run the test again. + +If it fails you're going to need to read the error message carefully to figure +out what went wrong, and then try again. + +If it passes, then you're ready to move to the next step. + +## Submit + +When everything is passing, you can submit your code with the following +command: + + $ exercism submit hello_world.rb + diff --git a/ruby/hello-world/README.md b/ruby/hello-world/README.md new file mode 100644 index 0000000..4101bf2 --- /dev/null +++ b/ruby/hello-world/README.md @@ -0,0 +1,45 @@ +# Hello World + +The classical introductory exercise. Just say "Hello, World!". + +["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) 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](http://exercism.io/languages/ruby). + +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 + + +## Source + +This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](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. diff --git a/ruby/hello-world/hello_world.rb b/ruby/hello-world/hello_world.rb new file mode 100644 index 0000000..483e534 --- /dev/null +++ b/ruby/hello-world/hello_world.rb @@ -0,0 +1,5 @@ +module HelloWorld + def self.hello + "Hello, World!" + end +end diff --git a/ruby/hello-world/hello_world_test.rb b/ruby/hello-world/hello_world_test.rb new file mode 100644 index 0000000..2ed1381 --- /dev/null +++ b/ruby/hello-world/hello_world_test.rb @@ -0,0 +1,43 @@ +begin + 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 +end + +# Common test data version: 1.1.0 be3ae66 +class HelloWorldTest < Minitest::Test + def test_say_hi + # skip + assert_equal "Hello, World!", HelloWorld.hello + end +end + +__END__ + +***************************************************** +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 +GETTING_STARTED.md. +*****************************************************