2. Continue method execution after the return statement

We tend to think that the return keyword always returns control back to the context of the method call, but actually we can still continue method execution if we want to. Here’s how: "ensure"block will be called just after "return" statement:

3. How to return more than one value from the method without explicitly wrapping it into a data structure?

It’s a pretty simple question, but as developers we rarely think of how much of syntactic sugar in Ruby goes unnoticed.

As an example, imagine that you have a method “authorize” that should return a status and some message with it. How would you write this method without wrapping it into an array like this “[status, message]”.

The solution is really easy, you just need to add an explicit “return” and pass multiple arguments to it separated by a comma:

This pattern can be easily implemented for any data structure and may be useful, for example, to store parsed XML documents or create Tree-like structures.

6. Pass block to a method without explicitly capturing it to a variable

This is my favorite trick in the list and it took me some time to figure out how it actually works. I found it in the source of one popular ruby gem called Faraday. It uses this code to build a connection object for request and passes block to the builder:

As you can see, `Faraday::Connection` expects to receive a block as a parameter for constructor, but the parent method doesn’t contain any reference to the block, and instead it creates `Proc.new` and assigns it to the block variable :|. Here’s how it works.

Proc.new

Creates a new Proc object, bound to the current context. Proc::new may be called without a block only within a method with an attached block, in which case that block is converted to the Proc object.

This means Proc.new implicitly captures the block if it’s given to the method.

But why this code was written this way seven years ago? It would be much easier just to add &block to the method signature and then pass it to the child method. For sure, this advanced technique exists for a reason—and that reason is performance.

Proc creation is considered an expensive operation in Ruby and when you pass &block as a parameter to a method, it always creates a new Proc, even if doesn’t pass the actual block.

So, for the sake of performance, you can check if a block is given, and then capture the block with Proc.new. This definitely decreases the readability of the code, but don’t rush to refactor your code just yet. Ruby 2.5 comes to the rescue and there is no need for this kind of optimization anymore!

That's all for now, folks!

I hope these tricks were useful for you and now you feel that you know a bit more about Ruby.

Like most professional developers, I usually try to avoid using any tricks in my own code to keep it simple and follow the principle of least surprise. Still, knowing these underlying details always helps to read and understand the code.

What are your favorite tricks and tips in Ruby? Share them in the comments!