The Wonderful World of ASCII Art — Ruby CLI

As a former Arts major, I enjoy nice looking things. While discovering my new passion for STEM, my love of aesthetics and storytelling has thankfully persisted throughout this unfamiliar process. One of the main factors that drew me to computer programming was the craft’s seamless ability to merge creativity and technicality. Amidst week 3 of my coding bootcamp journey I face the exciting obstacle of creating my first Ruby command-line (CLI) application, and as I get into the meat of ActiveRecord conventions, I know that for myself I have an additional goal for my project: I want it to look pretty. I’ve found that the use of ASCII art, which is essentially text-generated art, can help me achieve that beautifully. I intend to document a few ways that ASCII visuals can be displayed via the terminal in hopes of enhancing my program (and possibly yours!).

Before anything, in order to display an art piece, we need art! There’s a vast array of text and image ASCII art converters, as well as databases full of pre-made ASCII art scattered throughout the internet. Here are a few of my favorites:

Once you have your masterpiece, one of the first methods of implementing ASCII art into a program is to read it directly from a text file. Copy and paste your art into a text editor of your choice, save it, then display it! Here are some ghost buddies that are saying hello. I’ve saved them to a text file called “hello.txt.”

('-. .-.   ('-.                                  
( OO ) / _( OO)
,--. ,--.(,------.,--. ,--. .-'),-----.
| | | | | .---'| |.-') | |.-') ( OO' .-. '
| .| | | | | | OO ) | | OO )/ | | | |
| |(| '--. | |`-' | | |`-' |\_) | |\| |
| .-. | | .--'(| '---.'(| '---.' \ | | | |
| | | | | `---.| | | | `' '-' '
`--' `--' `------'`------' `------' `-----'

In this instance, I created a method that I can call to show the imagery.

def display_ascii_art 
# NOTE: /Users/username/ is a placeholder that refers to where you personally stored the file (file path). If you want others to be able to access your art text files, be sure to store the art within the same directory as your Ruby project!display_ascii_art

Here is reading the contents of the file (our ASCII picture), and we utilize puts to display it on the screen. Lo and behold, when we run our file we see our friendly apparitions.

Another way of displaying ASCII art data to the screen is to paste the art directly into your code! This is at the expense of having a more lengthy program and potential syntax errors, but it works if you don’t want to rely on external imagery. Using the aforementioned ASCII image converter, I’ve generated some art of a puppy and saved that to a variable within my Ruby program.

Your ASCII art is treated as any other string in Ruby.

IMPORTANT NOTES: Due to ASCII art’s classification as a string, when using it in your Ruby file make sure you either remove, or change any parentheses (“)/(‘) to backticks(`) to ensure your program doesn’t run into errors.

Similarly, keep in mind that in Ruby backslashes (\) are considered escape characters. Using “\n” within a Ruby string to create a new line is an example of this. In order to display ASCII art with backslashes instead of forward slashes (/) to the screen, make sure to use two!

puts "\\hello" # => \hello

As you can expect, when I run this program I see my digital puppy.

That covers the basics of getting images to show up, but things get really fun when we start to include the use of Ruby gems to further beautify our creations. Listed below are a few examples of gems that can be added to easily generate or enhance your command-line text visuals:

  • colorize — gives us colored text in our terminal
  • asciiart — a command line tool that turns images into ASCII art, similar to the online image to ASCII art converter
  • artii — a command line tool that generates ASCII art from text, similar to the online text to ASCII art converter

I’ve only grazed the surface of the many ways we can visually manipulate the command-line, make our program look more polished, and enhance the application experience. There is an entire world of ASCII that I encourage you to explore! Good luck to all of us on our respective tech journeys, and happy coding.

Lover of games, programming, and food. Consistently creating something that involves at least two of these.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store