Learn to Use Your Adjustable Wrench

The importance of focusing on fundamental programming concepts rather than Ruby method-hunting

The Ruby programming language comes with a ton of built in functionality, or methods, in the classes and modules of its Core API and there are even more available to use in the collection of libraries that form the Ruby Standard Library. As of Ruby 2.4.0 the Array class alone has over 80 instance methods.

For someone new to programming, and to Ruby in particular, this vast selection of methods can seem both impressive and intimidating in equal measure — a common question for beginners is ‘how am I going to remember all of these methods?’. There’s also a common habit among beginner Rubyists to go method hunting when trying to code a solution to a programming problem.

I got into this bad habit myself as a newbie Rubyist in the Launch School program. When I first started taking part in their Code Challenges, I’d often spend a long time coming up with a solution to the set problem only to then review the other submitted solutions and find that where I had used maybe 10 or 15 lines of code to solve a particular part of the problem another solution had achieved the same thing in one or two lines by using a method I had never seen before. My response at the time was ‘I wish I’d known that method! I need to go learn all of these!’

Although the instructors at Launch School regularly advise new students not to go method hunting, the temptation to do so is strong. Using a handy method is a great shortcut to having to figure out and then code a solution using a loop or a basic iterator.

Now, I’m not saying ‘never use methods’; the whole reason they exist in the first place is because they save developers a bunch of time on repeatedly implementing the same solutions to common problems. What I’m suggesting is that for a beginner, using these methods is not necessarily the most useful thing to do in order to learn Ruby well.

Simply invoking a method can almost seem like magic — you just call a method and it does this cool thing; but it’s a layer of abstraction, and all it really teaches you is how to invoke that method rather than giving you any deeper understanding of Ruby or of the fundamentals of how a programming language works.

We can think of the Ruby Core API as a tool-set. It has lots and lots of different tools, some of them with a very specific purpose that fit a fairly narrow use-case. When first learning your craft it is far more beneficial to your education to focus on the more general tools — such as your adjustable wrench — than to try to memorise a whole bunch of specialised tools — say, a size 6 hex-head reverse sprocket extractor (okay, so I just made that one up!) that you might rarely have any use for.

Let’s look at some examples of interleaving two arrays.

Using a loop:

Using an iterator method:

Using Array#zip and Array#flatten methods:

All of these pieces of code achieve the same results, and an experienced Rubyist would choose the combination of zip and flatten every time. If you’re taking your first steps in Ruby, or in programming in general, I would highly recommend that rather than reaching straight for a method like ‘zip’, that you get comfortable with using loops first of all; and once you’ve mastered loops get comfortable using basic iterator methods like ‘each’ and ‘each_with_index’.

Going via this longer route will teach you some of the fundamental concepts of a programming language that you might neglect by rushing to use a short-cut. In the long run this will have many benefits:

  • It improves your fluency with the basic syntax and helps you understand the ‘mechanics’ of the language better
  • It improves your problem solving abilities, since you actually need to think in detail about the implementation of your solution rather then simply calling a method and passing it an argument
  • These fundamentals are transferable to other programming languages, where there may not be equivalent methods or functions to those that exist in Ruby
  • You’ll be better equipped to deal with situations where you can’t use a method and have to code a more ‘manual’ solution. This could be because a method doesn’t exist for what you want to do, or perhaps it could be an interview situation where you’re asked to “interleave these two arrays without using zip” or “transpose this grid without using the Array#transpose method”.

So, yes Ruby gives you access to lots of really useful methods; if you’re learning to program though, before you rush to start using all the methods you should definitely learn to use your adjustable wrench.