Skip to content

Instantly share code, notes, and snippets.

@leonelgalan
Created March 22, 2014 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonelgalan/9712823 to your computer and use it in GitHub Desktop.
Save leonelgalan/9712823 to your computer and use it in GitHub Desktop.

The Beauty of Ruby (Part 2)

If you read the title and thought I was going to talk about the gemstone, you are in the right place. This series is intended for non-developers to understand why some of us, developers, prefer Ruby over other languages.

This is a series of blog posts, read part 1 to see Ruby examples against other languages: PHP and Java.

===

I often see code written like this:

if (user.sad === true) {
  greet(user);
}

Which makes sense, but alienates code by using so many symbols. Instead Ruby's way reads much nicer, even my mom could read it.

greet user if user.sad?

I asked my mom and she agrees. Notice how the method sad? ends with a question mark. This is called a predicate and it is a convention followed by Ruby developers to denote methods that return a boolean value. In most languages I knew before, this wasn't allowed.

Another great feature of the language, is allowing the "if statement" to be written after, this is called a statement modifier and makes it easier to read. My rule of thumb is to use a statement modifier for single line conditionals.

Of course, we could be as verbose and say:

if (user.sad == true)
	greet(user);
end

But no one writes Ruby like this, and if someone does, we should have the talk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment