Initial Commit
This commit is contained in:
1
ruby/hello-world/.version
Normal file
1
ruby/hello-world/.version
Normal file
@@ -0,0 +1 @@
|
||||
1
|
147
ruby/hello-world/GETTING_STARTED.md
Normal file
147
ruby/hello-world/GETTING_STARTED.md
Normal file
@@ -0,0 +1,147 @@
|
||||
# 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:
|
||||
|
||||
ESSS
|
||||
|
||||
Finished in 0.001539s, 2599.0903 runs/s, 0.0000 assertions/s.
|
||||
|
||||
1) Error:
|
||||
HelloWorldTest#test_no_name:
|
||||
NameError: uninitialized constant HelloWorldTest::HelloWorld
|
||||
hello-world/hello_world_test.rb:5:in `test_no_name'
|
||||
|
||||
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
|
||||
|
||||
### Understanding Test Failures
|
||||
|
||||
Whether you are on Windows, Mac OS X or Linux, you will eventually be faced with
|
||||
errors and failures that look a lot like the Mac OS X / Linux error above.
|
||||
|
||||
The letters `ESSS` show that there are four tests altogether,
|
||||
that one of them has an error (`E`), and that three of them are skipped (`S`).
|
||||
|
||||
The goal is to have four passing tests, which will show as four dots: `....`.
|
||||
|
||||
The tests are run in randomized order, which will cause the letters to display
|
||||
in random order as well.
|
||||
|
||||
## Step 3
|
||||
|
||||
Run the test again.
|
||||
|
||||
1) Error:
|
||||
HelloWorldTest#test_no_name:
|
||||
NoMethodError: undefined method `hello' for HelloWorld:Class
|
||||
hello_world_test.rb:5: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 6
|
||||
|
||||
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.
|
||||
|
||||
Open the hello_world_test.rb file, and find the word "skip". All but the first test
|
||||
start with "skip", which tells Minitest to ignore the test. This is so that
|
||||
you don't have to deal with all the failures at once.
|
||||
|
||||
To activate the next test, delete the "skip", and run the test suite again.
|
||||
|
||||
## Wash, Rinse, Repeat
|
||||
|
||||
Delete one "skip" at a time, and make each test pass before you move to the
|
||||
next one.
|
||||
|
||||
## Submit
|
||||
|
||||
When everything is passing, you can submit your code with the following
|
||||
command:
|
||||
|
||||
$ exercism submit hello_world.rb
|
||||
|
73
ruby/hello-world/README.md
Normal file
73
ruby/hello-world/README.md
Normal file
@@ -0,0 +1,73 @@
|
||||
# Hello World
|
||||
|
||||
Write a program that greets the user by name, or by saying "Hello, World!" if no name is given.
|
||||
|
||||
["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is the traditional first program for beginning programming in a new language.
|
||||
|
||||
**Note:** You can skip this exercise by running:
|
||||
|
||||
exercism skip $LANGUAGE hello-world
|
||||
|
||||
## Specification
|
||||
|
||||
The `Hello World!` program will greet me, the caller.
|
||||
|
||||
If I tell the program my name is Alice, it will greet me by saying "Hello, Alice!".
|
||||
|
||||
If I neglect to give it my name, it will greet me by saying "Hello, World!"
|
||||
|
||||
## Test-Driven Development
|
||||
|
||||
As programmers mature, they eventually want to test their code.
|
||||
|
||||
Here at Exercism we simulate [Test-Driven Development](http://en.wikipedia.org/wiki/Test-driven_development) (TDD), where you write your tests before writing any functionality. The simulation comes in the form of a pre-written test suite, which will signal that you have solved the problem.
|
||||
|
||||
It will also provide you with a safety net to explore other solutions without breaking the functionality.
|
||||
|
||||
### A typical TDD workflow on Exercism:
|
||||
|
||||
1. Run the test file and pick one test that's failing.
|
||||
2. Write some code to fix the test you picked.
|
||||
3. Re-run the tests to confirm the test is now passing.
|
||||
4. Repeat from step 1.
|
||||
5. [Submit your solution](http://help.exercism.io/submitting-exercises.html).
|
||||
|
||||
## Instructions
|
||||
|
||||
Submissions are encouraged to be general, within reason. Having said that, it's also important not to over-engineer a solution.
|
||||
|
||||
It's important to remember that the goal is to make code as expressive and readable as we can. However, solutions to the hello-world exercise will be not be reviewed by a person, but by rikki- the robot, who will offer an encouraging word.
|
||||
|
||||
* * * *
|
||||
|
||||
For installation and learning resources, refer to the
|
||||
[exercism help page](http://help.exercism.io/getting-started-with-ruby.html).
|
||||
|
||||
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.
|
||||
|
||||
In order to run the test, you can run the test file from the exercise
|
||||
directory. For example, if the test suite is called
|
||||
`hello_world_test.rb`, you can run the following command:
|
||||
|
||||
ruby hello_world_test.rb
|
||||
|
||||
To include color from the command line:
|
||||
|
||||
ruby -rminitest/pride hello_world_test.rb
|
||||
|
||||
The test files may have the execution bit set so you may also be able to
|
||||
run it like this:
|
||||
|
||||
./hello_world_test.rb
|
||||
|
||||
|
||||
## Source
|
||||
|
||||
This is a program to introduce users to using Exercism [view source](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program)
|
55
ruby/hello-world/hello_world_test.rb
Normal file
55
ruby/hello-world/hello_world_test.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env ruby
|
||||
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 xRuby track.'
|
||||
rescue LoadError => e
|
||||
puts "\nError:\n#{e.backtrace.first} #{e.message}"
|
||||
puts DATA.read
|
||||
exit 1
|
||||
end
|
||||
|
||||
# Test data version:
|
||||
# 7668b09 Added hello world test definition
|
||||
|
||||
class HelloWorldTest < Minitest::Test
|
||||
def test_no_name
|
||||
assert_equal 'Hello, World!', HelloWorld.hello
|
||||
end
|
||||
|
||||
def test_sample_name
|
||||
skip
|
||||
assert_equal 'Hello, Alice!', HelloWorld.hello('Alice')
|
||||
end
|
||||
|
||||
def test_other_sample_name
|
||||
skip
|
||||
assert_equal 'Hello, Bob!', HelloWorld.hello('Bob')
|
||||
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.
|
||||
*****************************************************
|
Reference in New Issue
Block a user