Tips for Programming in Ruby
Some of these tips apply to other programming languages
In the past 5 months, I’ve spent a lot of time studying a programming language called Ruby. Even though that little time of learning isn’t enough to certify me as a professional, I’ve come across some good tips and tricks that have helped me understand my lessons better and debug programs more efficiently. Let’s begin.
Debugging with Pry
Pry is a powerful Ruby REPL, way better than IRB. You see, I used to run my code on IRB, which would mean I’d have to type my code from the file I’m working on into the IRB over and over again so that I can make sure there are no errors. Pry eliminates that tedious task of writing code several times. It’s a very handy tool, I recommend it to everybody who’s learning or even developing in Ruby. Here’s more info about Pry.
Comments
In Ruby, you can leave comments throughout your program, so they will be ignored when you run the script. A good way to really understand a concept and dig deep is to put a comment at the end of each line explaining what it does and how it affects the program you’re writing.
This forces you to switch into a “teaching” mode, so it can be difficult at first. In programming, it’s important that one has the ability to explain their script step by step because if they can’t explain their own code in words, then they truly don’t understand the script.
Pseudo-Code
Pseudo-Code is almost similar to comments but backward. In pseudo-code, you’re writing code in an ordered list format, explaining how the script needs to function.
Once you do that, you have to translate this into actual code. See what I mean by “backward?” This is helpful because we’re structuring our pseudo-code which will make it very easy to translate it to real code. Once you do, you will already have a stable structure!
Flow Charts
When we program, we are literally stuck in our heads trying to take an idea out of our brain and code it. At high levels of programming, this is a terrible approach since there are lots of complicated algorithms the human brain can only bear to sustain. That is why I believe a flow chart can be very handy in these kinds of situations. Since we are in our heads, we have no way to visually see how we wish to write our code besides writing it down. Flow charts solve this problem.
Here’s a flow chart on how to pour a glass of milk. It looks pretty complicated for such a simple task, right? This is how a computer thinks. It sees everything as a flow chart. As programmers, we are forced to think like a computer, and it’s not easy (well, for most of us it’s not.) A flow chart can help significantly since we have a visual construct of how we want our code to function.
From my 5 months of programming, this is as much as I can offer. Try these tips out and see how well they work.
Thank you for reading.