Ruby CLIs and Unexpected Looping

Ariel V Grubbs
2 min readOct 26, 2020

--

Hello, my name is Ariel Grubbs, and this is the first of a series of blogs I will write about my experience learning programming. It will hopefully prove helpful to some of you out there who are also just getting started in the programming world, or are just looking to get another perspective.

So, with introductions out of the way, let’s get started with our topic for today: The Importance of Explicitly Ending Methods Before Calling Other Methods in Ruby. Not a very catchy title. I was recently creating a CLI project where my idea was to send the user into a chain of methods that would display choices for them and allow them to navigate through the application with a degree of freedom, and while making this program I kept running into the problem of my code looping back to an earlier point without the user telling it to do so. I looked high and low for where I could have accidentally called an earlier method later in the program, but there was no call like that.

After an extensive amount of banging my head against the wall, and attempts to work around it and make it a ‘feature not a bug’ I finally made progress when I realized that after calling a method from inside an earlier method, I never left the earlier method. The entire subsequent string of code would be executed from inside the earlier method, and only when the user reached th eend of the path, did the rest of that method execute. This often wreacked havoc on my if statement logic, for example: if you set up a simple if statement to test if the username the user entered corresponded to an existing user row in your database, and structured it to send the user to create a user row if it did not, and your code eventully returned without you meaning to, anything after the moment when you called the user creation method would execute, possibly including an else statement that would now see that the user does exist, and take them down another path.

I don’t mean to say that you couldn’t utilize this mechanic while building your own ruby application, I just thought that I would make this blog post to serve as a reminder for other beginners about the way Ruby reads programs.

--

--