What’s Ruby? (Wait, like the mineral?)
For anyone who doesn’t know, I’m in a coding Bootcamp at Flatiron. Now within the 3-month program, we are challenged to learn new languages, become proficient, and tested. Then after each phase, we complete a coding challenge. It sounds challenging yes, but at the same time, you get an adrenaline rush whenever you start a new language to learn. I’m now in my third phase and the language I’m currently learning is…. RUBY!
Now, to get to the point. I knew right off the bat that this language was going to be different. If you look into the history of Ruby it’ll become much more clearer.
Before we dive deeper let’s get a quick history lesson in the world of ruby.
History
Yukihiro Matsumoto otherwise known as “Matz” wanted to create a language during the 1990s that was easy to use but more importantly easier to read. He felt that it’s important for us to be able to understand how the code was being read in simple terms instead of memorizing the syntax(verbiage). A language that was easy for people to read as if it was a conversation with the computer not simply commands on a screen.
Usage
Now you know how the code might look like let’s take a look.
Javascriptfunction multiply (x, y) {
return x * y
}
multiply(5,5) --> 25
You might be thinking, “Hey I thought I was getting a quick intro to Ruby not Javascript so what's going on?” Well, the thing is, there’s actually a couple of similarities between JS and Ruby. But the example above to a person who is just getting into JS might have trouble reading it at first. Here’s where Ruby comes in.
def multiply(x , y)
return x * y
endmultiply( 5 , 5 ) --> 25
The difference here is that we’re making it much more clear for the person reading it what exactly we’re going to do. But let’s break it down line by line.
function vs. def
When writing functions in JS you have to almost always define a function (Which is just a set of instructions.) followed by the function name. In contrast with Ruby, you use def to “define” your functions. This makes it much easier to read because it’s using verbiage we use daily.
Brackets or no brackets?
The next thing we can see is that after the parameters (which also have options when it comes to using them.) we can see that in the Ruby example there are no brackets! This is because in Ruby we don’t have to have brackets. Now, this is not absolute. There are some cases where you will need to use brackets but as opposed to JS where it is necessary to include them in a function otherwise, it won’t work adds another later of readability to the reader.
Is this the End?
Lastly, we can see from this simple example that there is something in place of the brackets at the bottom of the code. The “end” keyword is used for well…ending the method. It’s at the bottom because it will execute the last line before then and once it hits the last line with stop running.
Conclusion
Things to note, things will get more complicated in Ruby, but only because it’s natural for us to be doing more and more complex tasks so using this easier-to-read syntax that will be a huge help in the end. Lastly, since Ruby is a back-end language meaning that you won't actually be seeing your hard work on the web page but mostly code on your terminal, it might be the reason you’re having some trouble with the language. But once you dive deeper into the gem world, you might find that it can be a very powerful tool!