Is Ruby the right programming language for beginners?
One of the biggest criticisms I hear lobbed against learning Ruby as a beginner language is that Ruby has so many different ways of doing things. People say that these different ways of doing the same thing makes it hard for beginners to learn.
To be specific, here’s one example of this argument that I read on reddit recently in response to my earlier post, Why Beginners Should Learn Ruby on Rails:
“Ruby almost always has 100 ways of doing any problem, and frequently the user-submitted examples on Stack Overflow are meant to dazzle rather than instruct.”
or
“So much shorthand and so many options that when I’m trying to learn one particular function, I get 100 different users telling me to do the problem 100 different ways, none of which helps me understand the thing I’m trying to learn.”
This is true.
Ruby in particular has lots of instances where there are multiple, equally good , ways of solving the same problem. But rather than that being a weakness of Ruby, the flexibility of the language is an incredible strength for beginners.
But rather than that being a weakness of Ruby, I actually think the flexibility of the language is an incredible strength, especially for beginners.”
You see, there’s no one way that you learn something, and whichever way sticks is usually the best for a beginner.
Here’s an example.
In Ruby, there’s the select method (in Ruby, functions are called “methods”), which lets you take a list of things and pull out a sub-set of that list.
It looks like this:
list_of_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list_of_numbers.select { |number| number.even? }
Even if you’ve never coded in Ruby in your life, read that second line and take a guess at what you’re going to get. That’s right, you’re going to get the sub-set of even numbers: [2, 4, 6, 8, 10]
There’s a second way to do the same thing. It’s called the find_all method.
So instead, you could write:
list_of_numbers.find_all { |number| number.even? }
What do you think you’ll get here?
Right! It’s exactly the same result as the select method.
So why does Ruby have at least two ways of doing exactly the same thing?
It’s because the people who created Ruby were inspired by a few other programming languages — Perl, Smalltalk, and Lisp are a few — and they borrowed the different names for doing things from those languages. Also, it happens to be really easy to write a method once in Ruby and just point other methods to it. That’s not as easy to do in other languages.
So why is this an advantage?
Well, because you don’t have to remember both of these ways of doing things! You just have to remember the one that works for you. I almost always use select.
Doesn’t this get confusing? Maybe, if you’re looking at lots of different people’s code. But by that point you’ve already learned that select and find_all are the same, and it’s not so hard to figure out. Language is a lot like this. There are many ways to say the same thing in English:
Language is a lot like this. There are many ways to say the same thing in English, which enables creativity.
I’m happy, I’m cheerful, I’m pleased, I’m delighted, I’m jolly, I’m beatific, I’m glad, … do I need to keep going?
Flexibility=Creativity
The flexibility actually opens up creativity, which I think is one of the coolest parts about a language like Ruby. Meanwhile, in most other languages there’s only one way of doing something. If you don’t remember that way then you’re screwed.
Another clear example that people like to hate on all the time: parentheses.
In Ruby, parenthesis are optional in methods. So are brackets { } a lot of the times, and you can basically use either single quotes or double quotes interchangeably.
Take that same list from before:
list_of_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
What if you wanted to see if it included a particular number?
You could use the include? method:
list_of_numbers.include?(2)
But you could also leave out the parentheses in this case (and lots of Rubyists do) because it’s prettier and easier to read:
list_of_numbers.include? 2
Sure, there’s a few rules about this. You have to put a space beforehand instead of parenthesis (otherwise Ruby would think you were trying to run a method called include?2 which doesn’t exist).
The point is, you can try many different ways of getting at the same issue, and get an answer to your question in many ways. Rather than get stuck on needing to know the exact way to write something, you can solve your problem in more than one way.
The same is true for software applications, like AutoCAD. As a user, you can type into the command line, navigate towards a button, or use drop-down menus. In addition, you can choose to draw a line with at least eight different tools (line edit, polyline, arc, points, etc). One way isn’t better than the other — they’re just all different tools for drawing. Having many different ways to get shit done makes it easier for people to remember the technique that works best for them.
Flexibility! It’s a beautiful thing.
I’ve never understood why people thought that having multiple ways of doing something was a bad thing. When you’re first learning, you have to grasp and understand whatever concepts you can, as a starting point.
Meanwhile, a language like JavaScript is a truly hard language for beginners to learn. This is because there are so many specifics that don’t come naturally to a beginner, and you have to do them that way. I’m not saying you can’t learn it, but the amount of times I’ve heard beginners so frustrated they were ready to give up because they’ve forgotten a “;” somewhere and have no idea where.
If you’re interested in reading more about why Ruby is a great language for beginners, check out Why’s (Poignant) Guide to Ruby. It’s one of the most beautifully and interestingly written pieces about programming. It is also one of the biggest reasons I fell in love with Ruby in the first place.
And then, if you want to learn more about Rails, watch our free one-minute video or check out our Rails course to go build your first app.