LearnWhy Ruby is Awesome


writes on October 12, 2017

I’ve been through a few programming languages over the years, and I can say without hesitation that Ruby is my favorite. Not the “best” (there is no “best” programming language), but my favorite.

Here’s why:

  • You can get the same things done in less code
  • It’s got lots of useful features built-in

Write Less Code

Here’s the classic “Hello, world” example in Java:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world");

Now, here’s a single line of Ruby code that does the same thing:

puts "Hello, world"

Here’s a list of symbols you’d have to understand in order to write the Java sample: public, class, {}, static, void, main, (), String[], System.out, println, ;

Here’s what you have to understand in order to write the Ruby code: puts (it’s the name of a method that prints stuff to the terminal), and "Hello, world" (it’s a string, a snippet of text).

Much simpler, right? I don’t want to suggest that every Java program is 6 times as big as an equivalent Ruby program, but Ruby programs are generally less verbose.

Useful Features

This code:

puts("delta alpha victor mike sierra".split.sort.map{|word| word.capitalize})

Produces this output:


Don’t worry if you don’t understand all that; it takes at least a few weeks of studying Ruby in order to learn how to write code like that. The main thing I want to show you is how much you can accomplish in a single line of code.

How can we make such a huge transformation using so little code? Because Ruby objects have lots of useful features built-in:

  • Ruby strings (like "delta bravo victor mike sierra") all have a split method that lets you split a string into an array (collection) of substrings. You can specify the character the split should happen on, but by default, it uses spaces, which is just what we need in this case. (Convenient, right?)
  • Now we have an array (collection) of individual words. Arrays all have a sort method that lets you sort their members. You can sort strings in reverse order, or by length, or any other way you want, but by default it sorts in alphabetical order. (Again, convenient, right?)
  • Now we have a sorted array of words. The last thing we do is call the map method (which, again, all arrays have) to apply some code to each word in the array and give us a new array with the results. That code is provided in a block (that’s the {|word| word.capitalize}). This particular block capitalizes each word.

And voila, we have a split, sorted, capitalized list of words, again using only one line of code!

That trick with the {|word| word.capitalize} block is particularly awesome, and is a unique feature of Ruby. You can do anything you want with the words in the array. For example, if I replaced capitalize with reverse, we’d get:


If I replaced capitalize with sub('e', '#'), we’d get:


That’s a lot of power for a single line of code!

But Wait, There’s More! (Much, much more!)

What I’ve shown you here is just a tiny fraction of the useful methods on strings and arrays. And we haven’t even talked about hashes, or classes, and I’ve barely explained blocks… All of these features and more can be combined in any way you want. Ruby gives you expressive power that many other languages just don’t have.

No wonder Ruby is the language behind the massively powerful Ruby on Rails framework! Creating code to handle complex tasks (like running a website) is just easier when you’re using Ruby.

P.S.: If you’d like to learn more about Ruby (at a much easier pace than this blog post) you should try out our newly-updated Ruby Basics course!

Start learning to code today with a free trial on Treehouse.


Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Get Started

4 Responses to “Why Ruby is Awesome”

  1. Jim Lesperance on October 21, 2017 at 1:33 am said:

    That’s Awesome the way you simplify Ruby. The language itself have a beautiful syntax and easy to learn that’s why we love Ruby as Developer

  2. Awesome compare.

  3. Thanks for the wonderful Informative post. If this Ruby decreases the lines of code & also doesn’t involve complexity then for sure this gonna rock.

  4. Great summary! Your Blogs’ always rock! Whenever I read the blog its so informative, I love it. One small thing – adding a method to a ruby object IS very similar to what can be done in JavaScript, but it has nothing to do with prototypical inheritance.

Leave a Reply

You must be logged in to post a comment.

man working on his laptop

Are you ready to start learning?

Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Start a Free Trial
woman working on her laptop