Project 1. Hello World

Installation

From a terminal application (like macOS Terminal or iTerm), clone the source code from the Github repository at scottserok/ruby_guide

~ $ git clone https://github.com/scottserok/ruby_guide.git

Change directory

~ $ cd ruby_guide/hello_world
~/ruby_guide/hello_world $

Usage

From the terminal, start an IRB session.

~/ruby_guide/hello_world $ irb
2.5.3 :001 > require './lib/hello_world'
 => true
2.5.3 :002 > hello
 => "Hello, world!"
2.5.3 :003 > exit

Require the Ruby file into the IRB session to make the defined hello method available.

Development

Run tests using Rake.

~/ruby_guide/hello_world $ rake test
Run options: --seed 26793

# Running:

.

Finished in 0.000845s, 1183.4319 runs/s, 1183.4319 assertions/s.

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

🎉

Now that you know your project has no failing tests you can move into developing new features. Before you do that you should check out a new branch.

~/ruby_guide/hello_world $ git checkout -b goodbye_world

This command will create a new branch in your local Git project and perform the checkout task. This is helpful to keep your master branch clean from any unfinished development work.

First you should write a test to specify how you expect the new feature to behave. Open up tests/hello_world_test.rb in your editor of choice and add a new test.

~/ruby_guide/hello_world $ open -a TextEdit tests/hello_world_test.rb
# tests/hello_world_test.rb
require 'minitest/autorun'  # require minitest library
require './lib/hello_world' # require our lib/hello_world.rb file

class HelloWorldTest < Minitest::Test
  def test_hello_world
    assert_equal "Hello, world!", hello
  end

  def test_goodbye_program
    assert_equal "Goodbye, program!", goodbye
  end
end

When you run our unit tests you should expect the new test to fail since you have not written our new method goodbye.

~/ruby_guide/hello_world $ rake test
Run options: --seed 59171

# Running:

E.

Finished in 0.000898s, 2227.1716 runs/s, 1113.5858 assertions/s.

  1) Error:
HelloWorldTest#test_goodbye_program:
NameError: undefined local variable or method `goodbye' for #<HelloWorldTest:0x00007fa20a07d880>
    /Users/scottserok/dev/serok-technologies/ruby_guide/hello_world/tests/hello_world_test.rb:15:in `test_goodbye_program'

2 runs, 1 assertions, 0 failures, 1 errors, 0 skips
rake aborted!
Command failed with status (1)
/Users/scottserok/.rvm/gems/ruby-2.5.3/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
/Users/scottserok/.rvm/gems/ruby-2.5.3/bin/bundle:23:in `load'
/Users/scottserok/.rvm/gems/ruby-2.5.3/bin/bundle:23:in `<main>'
Tasks: TOP => test
(See full trace by running task with --trace)

The key message in that output is NameError: undefined local variable or method 'goodbye'. You should now implement the new method to fix this error.

~/ruby_guide/hello_world $ open -a TextEdit lib/hello_world.rb
# lib/hello_world.rb
def hello
  "Hello, world!"
end

def goodbye
  "Goodbye, program!"
end

Now that the new method is implemented, you should not expect to see the same NameError error message when running the tests.

~/ruby_guide/hello_world $ rake test
Run options: --seed 36438

# Running:

..

Finished in 0.000934s, 2141.3276 runs/s, 2141.3276 assertions/s.

2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

Contributing

When you are satisifed with your changes to the source you will commit them to your branch.

~/ruby_guide/hello_world $ git add .
~/ruby_guide/hello_world $ git commit -m 'add goodbye_world method'

Now that you commited your changed files to your branch, you can switch back and for to and from the master branch without losing your work. There’s a high probability that your development team will require all changes to the master branch go through a review process. To share your branch with your team, push the branch to your remote Git repository.

~/ruby_guide/hello_world $ git push origin goodbye_world

This command reads, “push the branch named goodbye_world to the remote destination named origin.”

If you’re getting an error or are curious, then you can list the available remote desintation names with this command.

~/ruby_guide/hello_world $ git remote -v
origin	git@github.com:scottserok/ruby_guide.git (fetch)
origin	git@github.com:scottserok/ruby_guide.git (push)

Congrats! You’re a contributor 😃