Todo list Exercise

In case you get stuck anywhere, don't be afraid to ask the coaches! They are here to help and will gladly explain everything to you!

Take notes during the exercises. Even if you never look at them again, they will help you memorise things!

For these exercises, you will use your editor (Visual Studio Code or similar).

First, create an empty file with a meaningful name for this exercise, for example todo_list.rb. Note the file extension .rb! Also remember what folder you saved your file in. It makes sense to create a folder for each lesson of the course.

Then, use cd in the command line to change to that folder. When you run ls you should see your file there.

Now you can run your Ruby program with ruby todo_list.rb in the command line.

Repeat the above steps and remember to save your file in the editor before running it!

We’re building a Todo list program in the command line. We’ll do it step by step. After each step you will have a working program.

Hint: In programming, we use the verb implement a lot, e.g. implement this feature. It just means make it work. Write the code which will make it happen.

  1. Say hi to the user and ask for a command to enter. If the user enters "quit" you should print "Goodbye!". Otherwise, the command is ignored and your program ends as usual.

    Example interaction (user entered text is bold):

    > ruby todo_list.rb
    Welcome to the todo list
    Please enter your command!
    something
    > ruby todo_list.rb
    Welcome to the todo list
    Please enter your command!
    quit
    Goodbye!
  2. Now, allow the user to stay in your program. After the user entered her command, she should be able to type other commands. When she types "quit", the program quits. Otherwise she can enter commands indefinitely.

    Example interaction (user entered text is bold):

    > ruby todo_list.rb
    Welcome to the todo list
    Please enter your command!
    something
    Please enter your command!
    test
    Please enter your command!
    quit
    Goodbye!
  3. Now we want to be able to list todos. First, we prepare a todos variable and fill it with some example todos (e.g. "Buy milk", "Finish Rubymonstas exercise"). The user should be able to list all todos with the command "list". Our program now supports two commands: "list" and "quit". It will still ask for new commands if the last command wasn't "quit".

    Example interaction (user entered text is bold):

    > ruby todo_list.rb
    Welcome to the todo list
    Please enter your command!
    list
    Buy milk
    Finish Rubymonstas exercise
    Please enter your command!
    quit
    Goodbye!
  4. The command we implement next is "add". After the user typed "add" we will ask for a short text that is the new todo item. After the text was entered, we add this to the list of todos. After that, the user can enter a new command.

    Example interaction (user entered text is bold):

    > ruby todo_list.rb
    Welcome to the todo list
    Please enter your command!
    add
    What ​​is ​​your ​​todo?
    cycle home
    Please enter your command!
    list
    Buy milk
    Finish Rubymonstas exercise
    cycle home
    Please enter your command!
    quit
    Goodbye!
  5. The last command we implement is "done". If the user is done with a todo, she can type "done" and then enter the text of the todo. This todo item should then be removed from the todo list. After that, a new command can be entered as usual.

    Example interaction (user entered text is bold):

    > ruby todo_list.rb
    Welcome to the todo list
    Please enter your command!
    list
    Buy milk
    Finish Rubymonstas exercise
    Please enter your command!
    done
    What todo is done?
    Finish Rubymonstas exercise
    Please enter your command!
    quit
    Goodbye!