Tamagotchi and TTY-Prompt

Iain Robertson
6 min readSep 22, 2020

Attempting to build robust 90s-virtual-pets with a nifty Ruby Gem

The late 1990s (as with arguably any decade), was a time a great cultural, technological, and social shift. The VHS was at the peak of its popularity prior to the release of the DVD in ’97, America Online was the reason your landline wasn’t functioning, and Y2K was a wonderfully branded and succinct way to remind us all that computers were going to be confused by the Gregorian calendar — this was generally considered a bad thing that entailed stocking up on water and generators. And with that the stage was set for the Tamagotchi — a virtual pet housed within an egg-shaped keychain — to win the hearts and minds of children everywhere (all the while sneakily teaching them that they lack the maturity to care for another living thing), and it was on this wave of nostalgia that I began my first coding bootcamp project, and programmed my first CLI. What I perhaps failed to take into consideration when settling on this project, was the reality that making a virtual pet out of some sense of nostalgia entailed making an entirely functional and playable virtual pet.

CLI and TTY

A Ruby CLI (Command Line Interface) app, is actually a wonderfully convenient format to construct a tamagotchi clone. It entails a text based interaction with a user, and allows the user to send commands and values to the app that can be used to create, store, and manipulate the data necessary to construct and care for a virtual pet, the attributes of which are defined by our models and database migrations. The caveat with the convenience being that it falls upon you as the programmer to ensure an intuitive and robust experience for the user. Fortunately for all of us, the coding community consists of thoughtful and generous people like Piotr Murach, who created and made available TTY-Prompt.

TTY-Prompt, is an intuitive API that allows for the construction and implementation of a robust and user friendly CLI into your Ruby project. It provides all the tools necessary to achieve that goal by facilitating the assembly of easy to navigate menus and text based prompts, and the utilization of the inputs provided by the user into your code models and interface. All you have to do to incorporate its functionality is place gem “tty-prompt” within your ruby Gemfile, and then perform bundle install in your project terminal. Once that is out of the way you are free to create a new instance of tty-prompt by placing $prompt = TTY::Prompt.new, within the file responsible for your CLI.

In this case, it is assigned to a global variable, that can then be called upon whenever a prompt is necessary to acquire user input.

q.modify :down is ensuring that case sensitivity won’t cause issues with our user_name variable

Now obviously, you are free to use TTY-Prompt in any manner you see fit, but it is certainly worth mentioning that if TTY-Prompt is validating user inputs on your behalf, and styling the interface your users can interact with, you are now provided with ample opportunity to enhance the durability of your app and focus on building a more robust experience for the user. In this particular context, “robust” is a term used to refer to code practices that ensure your user stays on the right track and your code is written in such a way to limit avoidable errors based on absurd inputs or undesirable functionality.

Using If Statements to Reduce Errors

IF and ELSE statements preventing game breaking functionality

In this particular project, it quickly became evident that if statements are imperative to ensuring the desired functionality of the app. The database table containing values specific to a particular Tamagotchi’s stats have to be checked before proceeding with most methods. These values included integer values for :happiness and :hunger, and a boolean value of :alive. The latter value being especially important to preserve functionality. It simply doesn't make sense to have to ability to play with and feed a Tamagotchi that is no longer alive.

These concerns also extend to the user. In order for certain menu functionality to work, it is pragmatic to limit the number of Tamagotchis a user can adopt. By checking this value in the database before executing the “adopt” method, the user can effortlessly switch between their personal Tamagotchis via dropdown menu as opposed to contending with an unwieldy array of Tamagotchi instances.

Through the implementation of these if statements and TTY-Prompt validations it becomes a simple matter of planning and testing to ensure a robust and continuous experience for a user, the end goal being to limit errors maximize validations that will keep your project on the right path.

This project was enormous learning curve for me. I was unfamiliar with the concept of a CLI, models and their relation to object oriented programming were fresh terms to me, and I had never really given much thought to how I would approach creating an experience for a user. I believe one of the core concepts that I am going to carry with me after this project experience is an emphasis on ever more robust code that maintains the user experience. If nothing else, I do hope my experience with TTY-Prompt might come in handy again. And should you find yourself in the mood to build your own cutting edge 90s virtual pet, I have included some sample uses from my project below. Happy coding.

Sample TTY-Prompt Uses:

Menus:

Here we are able to create a dropdown menu, and dictate the corresponding method destinations through the use of if statements.

CLI menu

Text inputs:

We can utilize TTY-Prompt to set variables based upon text inputs from the user. Here q.modify :down is ensuring that the user input does not create any case sensitivity issues with their user name.

CLI prompt

Yes or no values:

Here TTY-Prompt is being utilized to get a true or false (boolean) value from the user, which can be used to redirect the CLI to a new set of prompts based upon this feedback.

--

--