5 Ruby Tips You Probably Don’t Know

Mehdi Farsi
Oct 2, 2018 · 3 min read

In this article we’re going to explore the following topics:

  • assigning the rest of an Array to a variable
  • array destructuring in block arguments
  • Hash#default_proc as default value
  • HEREDOC and method chaining
  • unary operators for non-numeric objects

Before to start

I’m thrilled to share with you our latest project: Fun Facts about Ruby — Volume 1

Please feel free to spread the word and share this post! 🙏

Thank you for your time!

Assigning the rest of an Array to a variable

When destructuring an array, you can unpack and assign the remaining part of it to a variable using the rest pattern

Array destructuring in block arguments

It’s possible to use the Array Destructuring mechanism in ruby blocks

produces

key1: value1
key2: value2
key3: value3
key4: value4

Here, each sub-array is destructured and the first and second entry values are assigned to the key and value block arguments.

Hash#default_proc as default value

A Hash.new can take a block that will be used to set the default value of a key

But what if we want to propagate this default value through all the entries and subentries of a hash ?

It’s possible to propagate the default block passed as argument of the Hash.new method to all the sub-entries of the freshly returned hash.

To do so we can use the Hash#default_proc method that contains the block passed as argument of the Hash.new method

Here, a new hash that takes a block as argument — which is used to define the default value of a new entry — is assigned to the layers variable.

When layers[:layer_1] is called without an explicit assignment, then the block passed as argument of the layers hash is executed.

This block is executed as following

In effect, the default_proc executes the block passed as parameter of the layers hash.

It’s same for the layers[:layer_1][:layer_2].

And then the layers[:layer_1][:layer_2][:layer_3] contains an assigned value. So the default_proc method is not called.

The default_proc method of the layers hash is propagated as default value of any new entries and sub-entries of this hash.

Inception…

HEREDOC and method chaining

As an HEREDOC is a multi-line string syntactic sugar, then it’s possible to chain methods on it.

In this example, we remove the trailing spaces and \n of an SQL query

Note that the squish method is defined within the Rails framework.

Unary operators for non-numeric objects

It’s possible to implement unary operators to an object by defining the -@ and +@ methods within the class declaration

produces

false
true

Voilà!

ONE MORE THING ⬇

Feel free to subscribe here: www.rubycademy.com

Thank you for taking the time to read this post :-)

Feel free to 👏 and share this article if it has been useful for you. 🚀

Also, as I post an article every 3 days, feel free to follow me to be notified of my new releases.

Here is a link to my last article: The Singleton module in Ruby: part II.

RubyCademy

E-Learning platform for Ruby and Ruby on Rails

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store