DEV Community

Cover image for Level Up Your Ruby Skillz: Writing Compact Code

Level Up Your Ruby Skillz: Writing Compact Code

Molly Struve (she/her) on May 29, 2019

We have all been there, we issue a Pull Request(PR) that we feel is super tight and awesome. It gets stamped "Approved", but there is a catch. Our ...
Collapse
 
abraham profile image
Abraham Williams

One small optimization I like is when chaining methods:

array.select{|n| n.even?}

can be shortened to:

array.select(&:even?)
Collapse
 
molly profile image
Molly Struve (she/her)

100% Thanks for pointing that out! I copied it from the other tutorial without even thinking that I should optimize it. Good call.

Collapse
 
andrewbrown profile image
Andrew Brown πŸ‡¨πŸ‡¦ • Edited

So I was mentoring the ExamPro interns a few days ago with an introduction on Ruby and I was showing how malleable Ruby is and we came up with this meme.

Collapse
 
enriquemorenotent profile image
Enrique Moreno Tent

The last expression would give a different result if x were to be false instead on nil. Beware.

Collapse
 
andrewbrown profile image
Andrew Brown πŸ‡¨πŸ‡¦

This is why I didn't originally include it since someone such as yourself would have spotted this. ha

Collapse
 
molly profile image
Molly Struve (she/her)

πŸ˜‚That's fabulous!

Collapse
 
andrewbrown profile image
Andrew Brown πŸ‡¨πŸ‡¦

In retrospect, the third should have been

x = 1 unless x
Collapse
 
enriquemorenotent profile image
Enrique Moreno Tent • Edited

Beware. This 2 lines do not do the same:

x = a.nil? ? b : a
x = a || b

Example:

a = false
b = "foo"

x = a.nil? ? b : a
# x = false

x = a || b
# x = "foo"
Collapse
 
nestorcustodio profile image
Nestor Custodio

Might I suggest you relabel the "Inline Booleans" section to "Conditionals Can Be Used As Modifiers", as "modifier syntax" is what that feature of the language is actually called.

Would make it easier for those who want to look up additional information on that.

Collapse
 
molly profile image
Molly Struve (she/her)

Oh, thanks, I will add that!

Collapse
 
drbragg profile image
Drew Bragg

Now I want to go back to all my old code and refactor everything that says x = a if x.nil? into x ||= a That's so awesome.

Collapse
 
omrisama profile image
Omri Gabay

Really cool for beginning Rubyists. I use all of these daily (Thanks Rubocop!) except for Safety Navigator which I'll hopefully start using more now.

Collapse
 
cescquintero profile image
Francisco Quintero πŸ‡¨πŸ‡΄

When I met Guard Clauses fell in love and now I can't stop looking for ways to use them and stop doing:

if condition
  # something
else
  # something else
end
Collapse
 
tfisher21 profile image
Tyler Fisher

This may be a month old, but I still use this cheat sheet nearly daily!

Collapse
 
molly profile image
Molly Struve (she/her)

YAY!!! I can't tell you how happy that makes me!!!! ❀️️

Collapse
 
tfisher21 profile image
Tyler Fisher • Edited

I finally just threw it on my bookmark bar instead of relying on browser history which made me realize I should throw up a comment ^_^

Collapse
 
lukewduncan profile image
Luke Duncan

You've done it again! This is awesome.