Why Should You Learn Ruby?

Knowledge Lighthouse
57 followers
7 Video Views·Oct 27, 2022
I've been a developer long enough to learn a few different programming languages,
but Ruby is my favorite. Let me show you why.
It might seem strange to start a video about Ruby with a Java program,
but I want to show you the way programs are written in other popular languages,
then show you the Ruby way so you can compare.
A common way of introducing students to a new language is to write a program that
displays Hello World on the screen, and that's just what this Java program does.
To run it, first we're going to have to compile it.
That'll convert this code, which is written in text humans can read,
to machine language a computer can understand.
We do that by clicking down here in the console area and running the javac command,
which stands for Java compiler, and giving it the name of this file.
So javac hello.java.
Now we can actually run the program by running another command named just java.
We need to give the java command the name of a Java class to run,
so we look here at the source code and see that the class is named Hello.
That's what we'll type here in the console.
Java hello.
The program runs, and we see the text Hello World printed here in the console.
Now let's look at a Ruby program that does the same thing.
The first thing you'll probably notice is that the Ruby program is much simpler.
It consists of just one line.
Let's try running it.
Here you'll see another nice thing about Ruby.
You don't have to compile the code as a separate step.
Ruby does it for you each time you run the program.
All we have to do is type the ruby command,
and then give it the name of the file we want to run, hello.rb.
Again, the program will run, and we'll see Hello World printed in the console.
The Ruby version was much simpler, right?
This is what's great about Ruby.
Its creators have put a lot of work into making it as easy to use as possible.
Now imagine coding an entire project in Java versus coding an entire project in Ruby.
Over the course of dozens of files, Ruby could save you a lot of typing.
Now, I don't want to pick on Java unfairly.
There's a reason it's designed the way it is.
The extra code helps ensure that every portion of a Java program is written in much the same way,
and the Java compiler makes finished programs run really fast.
I just want to show you that when the Ruby designers made choices about how programs
should be written, they chose to make things simpler and more pleasant for the programmer.
And that philosophy extends to every corner of the language.
Ruby is one of the most flexible programming languages out there.
It gets out of your way and lets you code the way you want to.
If that sounds appealing to you, Ruby would be a great language for you to learn.
Ruby's flexibility has allowed developers to create some incredibly innovative software.
There are tools like Chef, which Facebook uses to automate its server configuration,
or Sass, which helps provide styling for the websites of Pandora and Square.
Most importantly, Ruby powers Ruby on Rails,
which is arguably the world's most popular web framework.
Airbnb and Kickstarter use Rails to help run their websites, and so do many other companies.
Here's another thing that's great about Ruby.
Every piece of data that your program works with has useful snippets of code already attached to it.
In Ruby, pieces of data are known as objects,
and snippets of code attached to objects are known as methods.
Other languages have objects and methods too, but in Ruby, it's easier to make use of them.
I've created two programs that set up a list of numbers, and then sort that list.
One program is in Ruby, the other in Java.
You can see that I've run them both down here in the console, and both produce the same output.
But as you can see, the Ruby program is again much shorter.
In Java, the code to sort a list is stored in a separate bundle of code called a package,
which you have to load here at the top of the file before you can use it.
Then you have all the extra code to set up a class and a main method.
This is the same stuff that made the Java Hello World example much longer than the Ruby version.
Then we set up the list of numbers. Notice they're all out of order.
Then, and this is the interesting part, we have to go to the package of code we imported
to access a snippet of code that can sort our list of numbers for us.
Then, in the line of code below that, we have to go to that package of code again
to convert the list of numbers to a format that can be printed.
Compare that to the Ruby version. We don't have to load any separate bundles of code.
Oh, Ruby has those, they're just called modules instead of packages.
But in this case, and in many others, you don't have to load the modules yourself.
What you need is already loaded for you.
This is another example of a situation where the designers of Ruby had a choice,
and they chose to make things easier and more pleasant for the programmer.
So here's the line that creates our list of numbers.
And down here on the next line, we call a method named sort directly on the list object itself.
We don't have to go to a separate package. The code we need is right there on the object.
We also don't have to go to a separate package to convert the list to a printable format.
It's done for us automatically.
See how easy that was?
And that's just one example of the functionality that's available on every Ruby object.
Not only can lists sort themselves, but strings of text can capitalize themselves,
fractional numbers can round themselves to the nearest whole number, and much more.
Let me show you one more thing that's great about working in Ruby.
If the code libraries included with Ruby can't handle the problem you're trying to solve,
there are lots of other libraries available on the web.
These libraries are called Ruby Gems.
Ruby includes a tool that will automatically download gems for you,
install them on your computer, and make them available for use in your programs.
The most famous library distributed as a gem is Ruby on Rails,
but for this quick demonstration,
we'll install the gem for a smaller, simpler web framework called Sinatra.
To install it, all we have to do is type gem install sinatra.
The Ruby Gems tool will automatically download the Sinatra code and install it for us.
Up here is a Ruby program that uses the Sinatra gem to run a simple web server.
The require line at the top loads the code for the Sinatra gem.
This code here waits for a connection from any web browser,
and responds with an HTML level 1 heading saying hello web.
That's all we have to do.
We can run this program like any other, by typing ruby and then the file name.
rubywebapp.rb
It'll say that the Sinatra framework is waiting for connections,
so now we can connect from our browser.
We'll see the heading we set up showing our greeting.
Easy, right?
Most languages have package managers that will download and install code libraries like this for you,
but many of them require you to choose a manager program,
download it yourself, and install it yourself.
Ruby Gems is installed automatically along with Ruby,
so it's easy to get started using it.
Hopefully I've convinced you that Ruby is a great language to learn.
Now for the next question.
How should you go about learning it?
Understanding new technology is hard,
and one of the most difficult parts is figuring out what you need to know.
It helps if you have an expert guide to help you pick which parts to learn.
I'm a teacher at Treehouse, an online school where you learn at your own pace.
If you're new to software development, Treehouse is the best place for you to learn.
Although we have lots of content for professional developers,
we also offer carefully designed video tutorials for beginners
that don't assume you already know a lot about programming.
A great way to learn Ruby is to take Treehouse's Learn Ruby track.
Each Treehouse track is a guided curriculum designed by industry experts,
and it contains everything you need to know to get a successful start as a web developer.
Tracks are designed to help you learn effectively,
with content including video tutorials, quizzes, and code challenges.
You can get access to Treehouse tracks for just $25 a month.
You'll be able to code right along with the videos using Treehouse Workspaces,
a development environment that runs right in your web browser.
With Workspaces, you can write and run Ruby code,
even if you don't have Ruby installed on your computer.
I used Workspaces for all of the code samples I showed you in this video.
If you decide to learn a different programming language, we've still got you covered.
Treehouse has tracks for all these languages and more.
Picking a programming language track might seem like a tough choice,
but in the end, it doesn't matter which language you select.
All of Treehouse's tracks are set up to help someone who's learning to code for the first time,
and there are lots of similarities between the different programming languages.
Once you've learned one language,
you'll be able to learn any of the others much more easily.
We can't wait to work with you.
Click the link in the description to start your free 7-day trial at Treehouse,
and get started on your journey as a developer.
