Highlighting a few of Ruby’s built in methods

Andrew Smoker
3 min readMay 11, 2021

This week, I decided I would brush up on my Ruby skills and learn a few new methods in the process. In class, we learned a lot of the basic methods like .sort, .each, etc., but Ruby has many more available that we didn’t get to focus on. So today, I want to write about a few new (to me) Ruby methods!

&:
The first is a fairly simple shortcut where you can pass “&” to the beginning of a method argument which calls to_proc on its operand and passes it in as a block. You can add a to_proc method on any object including Symbols and that is where &: comes into play. It is especially helpful for enumerable methods like .each and .map. For example, let’s say I have a model called Salesman and want to get a list of all the uniq Salesmen names. I can use &: by calling

Salesman.all.map(&:name).uniq

Basically &:name evaluates to a Proc which does salesman.send(:name) and what I end up getting is the value of :name for each Salesman and adding .uniq on the end eliminates any duplicates. Very handy in cases like this and makes for very nice, clean code!

.minmax method
I didn’t know about the .minmax method, but I’m sure I will be using it in the future. The method assumes all objects implement Comparable and returns an array of the minimum and maximum values. So you can call it on an array to find either the min or max value very quickly. For example, if you have an array, a=[1,5,3,9,2], and call a.minmaxit will return[1,9]. By then calling [0] it will return the minimum or [1] the maximum.

.flat_map method
.flat_map works exactly like .map except it flattens the results into one array. So if you are dealing with nested arrays, but want your result to be flattened to one array, you can use .flat_map and it performs much better and faster than calling.map().flatten.

<=>
Finally, I wanted to look closer at the spaceship operator <=>. This is basically a three way comparison of <, =, > and compares two objects from left to right returning either a -1, 0 or 1. If the object on the left is smaller than the value on the right, it returns -1; if both objects are equal, it returns 0; and if the object on the right is greater it returns 1. The sort method uses the spaceship operator to figure out if it needs to swap or not from what is returned! It’s an important one to know and understand what is happening under the hood.

There are so many built in methods for Ruby, these are just a few examples of ones that I found. I’m definitely going to go through the docs and review all that Ruby has to offer!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Andrew Smoker
Andrew Smoker

Written by Andrew Smoker

I am 34 years old and making a huge career change by attending Flatiron School’s Software Engineering Bootcamp. Excited to learn!

Responses (1)

Write a response

This is useful! Thank you for sharing, Andrew!

--