Skip to content

Instantly share code, notes, and snippets.

@mikedao
Created August 16, 2016 20:18
Show Gist options
  • Save mikedao/3463dbe7fd8f1390d7d6840aca05c96b to your computer and use it in GitHub Desktop.
Save mikedao/3463dbe7fd8f1390d7d6840aca05c96b to your computer and use it in GitHub Desktop.

Exercises

Before gettign started, you'll need a few tools to work with user input and output.

  • How do we tell Ruby to print text to the screen?
  • How do we tell Ruby to bring in text from the user?

1. Basic puts / gets

Write a simple Ruby program which prompts the user to enter a message, then prints that message to the terminal. For example:

Type your message:
(user types "pizza" and presses enter)
pizza

2. Basic Branching

Extend your previous program so that if the text the user enters has an even number of letters, it prints "EVEN!", and if it has an odd number of letters, it prints "ODD!".

3. Multi-pronged branching

Write a new program that prompts the user for a message, then, depending on the following conditions, prints an appropriate message:

  • If the message ends with a consonant, print "CONSONANT!"
  • If the message ends with a vowel, print "VOWEL!"
  • If the message ends with a "y", print "DON'T KNOW!"

4. Easy Looping

Use a times loop to generate this output:

Line
Line
Line
Line
Line

5. Looping with a Condition

Build on your answer from the problem above and add an if/else to generate output like this:

Line is even
Line is odd
Line is even
Line is odd
Line is even

6. Three Loops

Generate the output below using three totally separate implementations (times, while and until):

This is my output line 1
This is my output line 2
This is my output line 3
This is my output line 4
This is my output line 5

7. Rando-Guesser

Write two implementations, one with while and one with until that output the following:

(The secret number is 8)
Guess is 4
Guess again!
Guess is 5
Guess again!
Guess is 9
Guess again!
Guess is 4
Guess again!
Guess is 8
You win!

The secret number and the guesses are both random numbers 0 through 10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment