Ahhh the command line… a powerful interface that served as the forerunner for the more intuitive and user friendly interfaces that were built thereafter (using the command line). The command line is, at first, mysterious. It doesn’t tell you how to use it. It’s just a blank field with a blinking cursor. But like a blank page, there are a world of possibilities waiting to be unlocked if you know how to put it to use.

This week I made an app for the command line. Nothing too fancy or ground-breaking, but it works well, I had fun making it, and the process got me excited to learn the next thing, and implement that, then the next thing, etc.. The bones and function of the app are fairly straightforward, and they didn’t take too long to build out, which left me a lot of time to tweak, reformat, and make the user interaction less stale (as much as I could for a CLI program). The build got me thinking — what are some ways we can make interacting with a computer feel less ‘computery’?

There’s a whole field of study dedicated to just that — giving software the ability to learn and assess like a human brain would. Giving software the ability to think imbues that software with the ability to respond to user interaction in what feels like a human or natural way. I’m fascinated with this concept, but I’m not quite there yet. That is, I’m not yet technically equipped to implement that kind of technology. So, my approach was a pseudo version of that human response feel. I focused on making varied responses to queries from the user, and I tried to make those responses seem natural and conversational. This comes in the form of a lengthy module with pre-formatted responses to user queries. Some of the pre-formatted responses still feel kind of robotic, and that’s something I’d like to get better at. When I started learning to code I didn’t realize that code-writing and content-writing were so intertwined.

To give you some details on the project — the program requests data about countries from an API, parses that data into JSON, makes country objects from the JSON objects using a class (called Country), then returns formatted information about countries when the user asks for it. I wrestled with how to instantiate these Country instances so that everything was formatted correctly, and so I wasn’t using a bunch of unnecessary lines of code. My first approach was messy — I passed attributes to Country’s initialize method as plain old unformatted strings, then formatted those strings individually in the initialize method. This basically doubled the lines of code I had to write in order to instantiate Country instances. I sought some advice from my instructor — I wanted to clean up my initialize method, and the cleaner approach was looking at me in the face the whole time — metaprogramming. Duh. I switched everything up to extract the desired data from the JSON object, then pass it to a hash (formatting it in the process), then pass that hash to Country’s new and improved initialize method (that uses an each loop on the hash). This made it easier down the line when I wanted to add more accessible attributes to each instance of Country. Isn’t abstraction great?

The ‘name’ attribute of each country took some tinkering to format correctly as well. Some of the JSON objects had names that were formatted like: “Tanzania, United Republic of” or “Congo (Democratic Republic of the)”. Those names definitely didn’t feel natural. Enter Regex, and some more help from my instructor (thanks Annabel!)… I set up an if statement that does the following:

  1. Checks if there are parentheses or commas in the JSON country name
  2. Splits the string into an array at the comma or the open parentheses
  3. Checks the array to see if the stuff AFTER the comma or open paren contains “of” or “the” (for cases like: “Saint Martin (French part)” / “Saint Martin (Dutch part)” that I wanted to leave alone)
  4. Swaps the first and second elements in the array
  5. Gets rid of the closed paren
  6. Joins the resulting array back together and overwrites the original name with the newly formatted name

The result is “Congo (Democratic Republic of the)” ⇒ “Democratic Republic of the Congo”. Pretty nifty huh? Here’s the code:

Can’t wait to get started on the next project!