Photo by NASA on Unsplash

Mercury’s Little Helper

Creating Retrograde Chaos Using Ruby

Chris Pine
4 min readNov 9, 2021

--

📚 Connect with us. Want to hear what’s new at The Pragmatic Bookshelf? Sign up for our newsletter. You’ll be the first to know about author speaking engagements, books in beta, new books in print, and sales that give you discounts of up to 40 percent.

Until recently, Mercury was in retrograde. Apparent retrograde motion is when it appears that a planet has reversed direction and is moving backward from its normal orbital motion. Planets never actually reverse direction like that, but because we are on a planet that is also moving, this optical illusion can occur on occasion. (Kind of like how if you are next to a car that starts moving when you didn’t expect it to, it can feel like you’re actually moving in the opposite direction.)

According to astrology, this particular optical illusion also has the power to create chaos in our lives, particularly in the areas of communication and, by extension, electronics. Looking at my own life recently, I feel like this could explain a lot! But apparently, Mercury hasn’t been in retrograde for the last year and a half, so maybe it was just normal pandemic stuff after all.

Helping Mercury Create Retrograde Havoc

Simon Blocquel (1780–1863, under the pseudonym Julia Orsini)
Public domain, via Wikimedia Commons

Whether or not Mercury’s apparent retrograde motion causes your computer to go haywire is up for debate. But as coders, we can make our dreams (and fears, and superstitions) come true!

If we could programmatically determine if Mercury is in retrograde, we could put that in a method and then use that method whenever we want to assist Mercury in causing a little havoc in our programs.

In Ruby, the code would look like this:

if (merc_in_retro())
# add random row to the user database with silly name
end

🚫 PSA: For real, don’t do this. This is pure evil right here. Having said that… let’s go see how it’s done!

Finding Out If Mercury Is In Retrograde

Image by iJStudio on Shutterstock

We’re going to use this API to find out if Mercury is doing its thing.

If you don’t know what an API is or how to use one, I explain it in Chapter 12 of my book, Learn to Program, Third Edition.

But the short version is that we’re going to write code to ask another computer if Mercury is in retrograde, a computer that is specifically set up to answer that question.

Here’s the basic code:

require "net/http"
require "json"
def merc_in_retro()
url = URI("https://mercuryretrogradeapi.com")
json_response = Net::HTTP.get(url)
hash_response = JSON.parse(json_response)
hash_response['is_retrograde']
end

In the first two lines, we’re telling Ruby we require some extra abilities in our program: we need to be able to make requests to other computers using HTTP, and we need to be able to convert objects to/from JSON, because that’s how this API responds.

Then we define our evil little merc_in_retro() method. It’s just a few short lines of code that do the following:

  • Specify the URL of the API we want to talk to.
  • Fetch the JSON data from that URL.
  • Convert the JSON into a Ruby hash.
  • Return the 'is_retrograde' value from that hash.

💥 Important: Keep in mind that every time you call this method, you are making a connection to another computer.

Since you are connecting each time, this method call will be thousands or millions of times slower than most method calls. Still super fast, but if you were to put it inside a loop, suddenly your program would get much, much slower. (It’s also not very polite to the kind folks who created this free API for us all to use.) So call this method only once, and if you need to use the resulting value in more than once place in your code (or in a loop) then just save the value in a variable and use the variable. That way is super fast and super polite.

And that’s it! Now you can use this method any time you want to assist Mercury in creating a little temporary havoc.

☿️ You’re welcome, Mercury!

--

--