Skip to content

Commit

Permalink
complete readme
Browse files Browse the repository at this point in the history
  • Loading branch information
SophieDeBenedetto committed Sep 10, 2015
1 parent 3c5317c commit 896689c
Showing 1 changed file with 102 additions and 32 deletions.
134 changes: 102 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
# Greeting Cli

## Goals

1. A simple CLI lab that requires no logic, only interpolation.
2. It demonstrates the structure of a CLI lab and having a bin use a method defined in a library.

## Outline

1. Describe Overview, building the first part of a complex military program. The first thing you need to do is ask the user for their name. After taking the user's name, the program should greet the user with "Hello, <user's name>. How are you doing?."
2. Screenshots / video of working program.

2. Structure of program. Walk user through what code goes where, the lib and the bin.

3. Have them first build the greeting method and use the test suites to make that pass.

4. They should then build the program in `greet` to load that file with require_relative.

5. Greet should output a hello and ask their name.

6. It should take input.

7. They need to use that input in greet.

8. Show them how to walk through the errors in the CLI spec.

Joshua Wargames clip. https://www.youtube.com/watch?v=7R0mD3uWk5c

## Objectives

1. Build a CLI (command line interface) application.
Expand All @@ -34,21 +8,27 @@ Joshua Wargames clip. https://www.youtube.com/watch?v=7R0mD3uWk5c


## Overview
![http://readme-pics.s3.amazonaws.com/2001_A_Space_Odyssey_Style_B.jpg]()

The year is 2001, humanity has discovered a mysterious object buried beneath the Lunar surface. Before Earth's most capable (and heroic) scientists can board the *Discovery One* and investigate, we must program the super-intelligent computer, HAL 9000. Luckily for us, however, most of HAL's functionality is already up and running. Our job is program HAL's greeting to the user.
<table style="margin-left: 30%"t>
<caption align="bottom" style="font-size: 12px">Theatrical release poster by Robert McCall</caption>
<tr><td><img src="http://readme-pics.s3.amazonaws.com/2001_A_Space_Odyssey_Style_B.jpg"/></td></tr>
</table>


The year is 2001, humanity has discovered a mysterious object buried beneath the Lunar surface. Before Earth's most capable (and heroic) scientists can board the *Discovery One* and investigate, we must program the super-intelligent computer, HAL 9000. Luckily for us, however, most of HAL's functionality is already up and running. Our job is to program HAL's greeting to the user.

Our program will:

1. Greet the user.
2. Ask the user to input their name.
3. Capture and store the user's input using the `#gets` method.
4. Use that captured input to puts out a string that greets the user by name, using string interpolation.
4. Use that captured input to `puts` out a string that greets the user by name, using string interpolation.

Check out the video below to see the final product in action:
Check out the video below to see the final product in action:

<iframe width="560" height="315" src="https://www.youtube.com/embed/sBQBP1Aaxzk" frameborder="0" allowfullscreen></iframe>


Notice that the the program is run with `ruby bin/greet`––we are using ruby to run the code in the executable file that lives in our `bin` directory. Let's take a closer look at our project structure now.
Notice that the the program is run with `ruby bin/greet`––we are using Ruby to run the code in the executable file that lives in our `bin` directory. Let's take a closer look at our project structure now.

## Project Structure

Expand Down Expand Up @@ -82,4 +62,94 @@ The `lib` directory is where we place the code that our program relies on to run

This directory contains the tests for our program. We have tests for the code in the `lib` directory in `spec/01_greeting.rb` and tests for the CLI, i.e. the user interaction portion of our application, in `spec/02_cli_spec.rb`. We'll use the test output from running `learn` along with the guidelines below to get our program working.

## Instructions

### Part I: The `greeting` Method

Before we worry about building the CLI, we need to build the core functionality of our program––that is, the actions that will be carried out once we obtain the user's input. That code belongs in the `lib` directory.

Open up `lib/greeting.rb`. This is where we'll be writing our code. This part of the lab is test-driven. So, run the test for *just this section* by typing `spec/01_greeting.rb` in your terminal.

In order to get our tests passing, we'll need to define a method, `#greeting` that takes in an argument of a person's name and uses string interpolation to return `"Hello #{name}. It's nice to meet you."`.

Once you get these tests passing, you're ready to move on to the next section.

### Part II: The CLI

Our CLI code belongs in the `bin` directory. Open up `bin/greet`. Notice that, according to convention, our executable file does not have a file extension (such as `.rb`). Take a look at the first lines of the file:

```ruby
#!/usr/bin/env ruby
require_relative '../lib/greeting.rb'
```
First, we have our shebang line that tells the terminal which interpreter to use to execute the remainder of the file. Second, we are requiring the `greeting.rb` file, from within the `lib` directory. This gives our executable file access to whatever code we write in that `lib` file.

If we run `learn` we can see from the test output that the following is required:

1. Use `#puts` to output the string "Hi! I'm HAL, what's your name?"
2. Use the `#gets.strip` method to store the user's input and set it equal to a variable called `name`
3. Call the `#greeting` method with an argument of the user's name (captured using `#gets` in step 2) to output the interpolated welcome string.

### Reading CLI Error Messages

Let's walk through some of the error messages from running the CLI spec together.

In your terminal, once you have your `spec/01_greeting.rb` tests passing, run `rspec spec/02_cli_spec.rb`.

The first failure and error message should look something like this:


```bash
Failures:

1) ./bin/greet executing a CLI Application outputs a welcome message and asks the user for input
Failure/Error: expect($stdout).to receive(:puts).with("Hi! I'm HAL, what's your name?"), "Make sure bin/greet has code that can output 'Hi! I'm HAL, what's your name?' exactly."
(#<IO:0x007fafb88ca568>).puts("Hi! I'm HAL, what's your name?")
expected: 1 time with arguments: ("Hi! I'm HAL, what's your name?")
received: 0 times
# ./spec/02_cli_spec.rb:7:in `block (2 levels) in <top (required)>'
```
The portion that is significant for us comes at the beginning:
```bash
1) ./bin/greet executing a CLI Application outputs a welcome message and asks the user for input
Failure/Error: expect($stdout).to receive(:puts).with("Hi! I'm HAL, what's your name?"), "Make sure bin/greet has code that can output 'Hi! I'm HAL, what's your name?' exactly."
```

The first part is a description of what is being tested. It means "running ./bin/greet to execute the CLI application should output a welcome message and ask the user for input".

Then, we see the description of our failure:

```bash
Failure/Error: expect($stdout).to receive(:puts).with("Hi! I'm HAL, what's your name?"), "Make sure bin/greet has code that can output 'Hi! I'm HAL, what's your name?' exactly."
```

The helpful part of this error message can be found at then end, after the `, `.

```bash
"Make sure bin/greet has code that can output 'Hi! I'm HAL, what's your name?' exactly."
```

The error message is letting us know that what we need to do to get the test passing.

Let's take a look at our second error message:

```bash
2) ./bin/greet executing a CLI Application uses #gets.strip to capture the user input and set it equal to a variable called name
Failure/Error: name = get_variable_from_file("./bin/greet", "name")
NameError:
local variable `name' not defined in ./bin/greet.
```
Once again, it starts with a description that means "Running ./bin/greet should execute a CLI application that uses the gets.strip method to capture the user's input and set it equal to a variable called 'name'".
The important part of our error message is at the bottom of the `Failure/Error` portion of the text:
```bash
local variable `name' not defined in ./bin/greet.
```
This is telling us that the `bin/greet` file fails to set a variable called `name` equal to the user's input that is captured via the `#gets.strip` methods.
Now that you have a basic sense of how to read these error messages, as well as some guidelines for getting those tests passing, go ahead and get HAL working properly!

0 comments on commit 896689c

Please sign in to comment.