A compendium of obscure Ruby methods (or maybe just a couple methods/concepts that I didn’t know before…)

Yisrael Gurkow
5 min readAug 2, 2021

--

Ruby has a lot of methods that aren’t used very often mostly because they don’t have very many use cases. As a result, for the cases where they are useful, a lot of people might simply not know about them because they never used them.

I decided to put together a list of ruby methods that aren’t so common. This way if you ever want to know if Ruby has a method that can help your program, or if you see a method you aren’t familiar with, you can come to one easy place and find the information you need.

Now to the list:

Some argument options:

1) Argument destructuring:

def print_from_array((a1,a2))
print first: a1, second: a2
end
print_from_array(['output1', 'output2'])
=> {first: 'output1'}, {second: 'output2'}

If you add a second set of parentheses around the arguments when you define the method like so => “ method((a1,a2))” this will destructure an array passed in as an argument to this method. As you can see on line 5, even though I am only passing in one argument to print_from_array (an array) the print_from_array method understands “a1” and “a2” to be elements in the array and prints out the individual elements. (Note: This doesn’t work with a hash - def print_from_array({a1,a2}).)

In the inverse, if you would define the method like this:

def print_from_array((a1,a2))
print first: a1, second: a2, third: a3
end
print_from_array(['output1', 'output2', 'output3'])
=> NameError (undefined local variable or method `a3' for main:Object)

you get an error even though you passed in an array of 3 elements.

2) Converting arguments into an array:

def convert_args_to_array(*args)
print args
end
convert_args_to_array("string1", "string2", "string3")
=> ["string1", "string2", "string3"]

This will convert any arguments passed in to “convert_args_to_array” into an array even if they are passed in as something else.

You can also do this:

def convert_some_args_to_array(arg1, *args)
print arg1, args
end
convert_some_args_to_array("string1", "string2", "string3")
=> string1 ["string2", "string3"]

Which will separate the first argument and then convert the rest into an array.

2. Some array methods:

1) ‘&’ — The intersection operator (also ‘.intersection’):

array1 = [1,2,3,4,5,6,7,8,9] 
array2 = [1,2,3,4,'a','b','c']
array3 = ['a','b','c','d','e']
array1 & array2 => [1,2,3,4]
array2 & array3 => ['a','b','c']
array1 & array3 => []

If you have 2 arrays and you want to find the elements that both arrays have in common (the intersection), you can use this operator to return a new array with elements that just the shared elements.

2) ‘-’ -The difference operator (also ‘.difference’):

array1 = [1,2,3,4,5,6,7,8,9] 
array2 = [1,2,3,4,'a','b','c']
array3 = ['a','b','c','d','e']
array1 - array2 => [5,6,7,8,9]
array2 - array1 => ['a','b','c']
array2 - array3 => [1,2,3,4]
array3 - array2 => ['d','e']

If you want to find the difference between 2 arrays you can use Difference operator to return a new array with only the elements from the first array that are not in the second array.

3) Rotate:

array = [1,2,3,4,5]
array.rotate => [2,3,4,5,1]

This method just rotates the entire array so that the original first element now becomes the last element.

On a similar note there is -

4) Shuffle:

array = [1,2,3,4,5]
array.shuffle => [5, 4, 2, 1, 3]
array.shuffle => [3, 4, 5, 1, 2]
array.shuffle => [5, 1, 3, 2, 4]

This will return a new array with the a randomly ordered array of the given elements. every time it will shuffle the order again to return a different array. (Can be useful for games and the like.)

3. Proc.new (or ‘proc’) & Lambda (->{code block}):

This can start off as a somewhat obscure concept but they are a very important part of Ruby (and many other languages as well).

To quote ruby-doc.org:

A Proc object is an encapsulation of a block of code, which can be stored in a local variable, passed to a method or another Proc, and can be called. Proc is an essential concept in Ruby and a core of its functional programming features.

I am afraid I won’t be able to do this topic justice so i will post a couple links for those who want more information.

Basically a Proc is a block of code which can be saved to a variable and later invoked with ‘.call’. It usually looks like this:

add_one = proc {|number| number + 1}
add_one.call(5) => 6

You may be familiar with this syntax from other methods such as ‘.map’ or ‘.filter’, but they are really utilizing a Proc (another way of saying ‘a block of code’) to tell them what logic to do on each iteration.

for further reading you can check out the ruby-docs or rubyguides.com.

Something to note on this topic is the ‘&’ Proc operator. Different than the Intersection operator mentioned earlier in this post, when used as an argument to a method, this can be called the ‘block deconstruction operator’ and indicates to the method that the argument being passed is Proc (a block of code) and should be treated as such.

Typical syntax for this might be:

p = proc {|x, y| x + y }

[[1, 2], [3,4]].map(&p) #=> [3, 7]

Notice that this a regular .map method but instead of defining the code block after ‘map’ we are defining the proc on the first line and passing it to map using ‘&’ to tell map that this is a block and map should use this for the logic on each iteration.

Conclusion:

I collected here a couple of methods or concepts that I found to be helpful or intriguing. Some of these I wished I had found out about earlier because they came in handy those couple times that I couldn’t find the right method for the job until finally someone brought the correct method to my attention. And some of them I just found to be interesting and I want an easy place to reference back to them should they ever be needed.

I hope to update this blog with more useful methods when I find them, to make this a more comprehensive resource for all those Ruby devs that are looking for just the tool for the job.

Attached are some useful resources for more information on these topics.

--

--

No responses yet