DEV Community

Cover image for Idiomatic Ruby: writing beautiful code

Idiomatic Ruby: writing beautiful code

TK on December 04, 2018

Ruby is a beautiful programming language. According to Ruby’s official web page, Ruby is a: “dynamic, open source programming language with a fo...
Collapse
 
ben profile image
Ben Halpern

Fabulous post. I’m not sure I’ve read a post that expresses the wonder of Ruby so coherently.

Collapse
 
databasesponge profile image
MetaDave 🇪🇺

Nice.

A common problem that I have been enjoying the use of tap on is populating an array with optional elements.

Instead of:

[
  name,
  (address if address),
  (phone if phone)
].compact

... (which creates two Array objects) I have switched to ...

[].tap do |ary|
  ary << name
  ary << address if address
  ary << phone if phone
end

... or ...

[name].tap do |ary|
  ary << address if address
  ary << phone if phone
end

As is often the case, simple examples don't really do this justice. When you have a lot of complexity about what is going to be added and when, it comes into its own.

Semantically, what I particularly like is the way that tap lets you use a block to say:

  1. I will now be doing something to this object
  2. Now I am doing it
  3. It is done
Collapse
 
jsrn profile image
James

Lots of hot tips in here!

I like to use the rubocop gem to watch my back when I'm writing ruby. It will point out a lot of non-idiomatic bits of your code and suggest ways to improve them in line with this great article.

Collapse
 
arjunrajkumar profile image
Arjun Rajkumar

Awesome! I love programming with Ruby too.

Recently discovered that the .any method can be used as an iterator too.
results.any? do |result|
end

Look forward to more.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Correct me if I'm wrong, but [1, 2, 3, 4, 5].select(&:even?) should actually be slower than explicitly writing a block, as it creates a Proc object implicitly by calling to_proc on the Symbol. There might of course be some optimization going on in the background that I don't know about. (EDIT: I totally agree that it looks neater though, and I use it a lot myself)

Collapse
 
richjdsmith profile image
Rich Smith

This is an awesome list of a bunch of great tips you've put together! Thank you for it!

Ruby was (and retrospectively, I'm thankful for it) the first programming language I learned. I've since picked up PHP, JavaScript and am working on Elixir, but I consider myself a proud Ruby programmer.

With the progress being made on the Ruby 3X3 goal, as well as the fact that nothing I have tried comes close to the productivity i get from working with Rails, I don't see that changing anytime soon.

I'm a Rubyist, and your list is a perfect example of why.

Collapse
 
sbaron24 profile image
sbaron24

Thanks for the post! In the following example under Select...

even_numbers = [1, 2, 3, 4, 5].map { |element| element if element.even? } # [ni, 2, nil, 4, nil]
even_numbers = even_numbers.compact # [2, 4]

...I noticed that after the first line, even_numbers is:

[nil, 2, nil, 4]`

Why does element if element.even? return nil when element.even? is false?

~s

Collapse
 
stenpittet profile image
Sten • Edited

Thanks a lot for writing this! I've been looking for ways to use the same filter that ES6 offers but couldn't figure out the syntax in ruby. This post is now bookmarked for reference!

Collapse
 
brotherbain profile image
John Gaskin • Edited

Nice article! It's worth pointing out that safe navigation is not a replacement for try, though. I've made this mistake a few times and had it bite me :D

try vs safe nav

Collapse
 
passthejoe profile image
Steven Rosenberg

Thank you very much for this tutorial. There are so many tips here ...

Collapse
 
teekay profile image
TK

Thanks! Glad you liked it!

Collapse
 
thadeu profile image
Thadeu Esteves Jr

you can as example a programatic lambda

lambda = ->(user) { user.id == 1 }
users.each(&lambda)
Collapse
 
rhymes profile image
rhymes

Tap is my favorite!

Great intro :)

Collapse
 
sudiukil profile image
Quentin Sonrel

Awesome post!

This really captures why I love Ruby so much: it's really expressive and natural when done right.

Collapse
 
camfilho profile image
Carlos Augusto de Medeir Filho

The Idiomatic Ruby cheat sheet

Collapse
 
chasestorydev profile image
chasestory

This Post Is Fantastic... Very clear, well explained.

Thank You!!

Collapse
 
isalevine profile image
Isa Levine

totally love this--i came back to programming after something like a 15-year hiatus, and ruby was just what i needed to fall back in love with coding (and see it for the linguistic art it truly is!)