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... [Read Full]
markdown guide
 

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

 

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.

 

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
 

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.

 

Very nice. As a PHP developer, I died once again. :-)

 

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)

 

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.

 

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!

 

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

 

you can as example a programatic lambda

lambda = ->(user) { user.id == 1 }
users.each(&lambda)
 
 

Awesome post!

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

 

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

Thank You!!

 

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!)

code of conduct - report abuse