4 Ruby Methods You Didn’t Know You Needed

Interesting methods hiding in the Ruby powerhouse

Tate Galbraith
Nov 14, 2019 · 4 min read
Image for post
Image for post
Photo by Nine Köpfer on Unsplash

I have a special passion for the Ruby programming language, as it was the first language I learned. Ruby is pragmatic and concise yet highly extensible. The syntax and composition is pleasing to look at, and there is also a wide base of support for the language.

I wanted to compile a fun list of interesting methods I’ve come across during my time developing with Ruby.

Ruby has a lot of built-in methods. Some methods are more useful and frequently used, while others are more ephemeral or not initially clear what their use might be. Below we’ll go over a list of methods that lie somewhere in the middle of these two extremes.

Note: The title of each section contains a link to the relevant APIdock page.

First up, we have one of the most useful Ruby methods around. This method is extremely helpful for troubleshooting and debugging complex code. tap allows you to quite literally tap right into the middle of a chain of methods and redirect some of the output.

Think of this like using pipes in Bash. You can pipe the output of the left side to the right and redirect it.

The link above has more detailed information on how to use the tap method, but below is a simple example of its capability:

[1] pry(main)> var = "string".reverse.tap { |x| puts x }.upcase
gnirts
=> "GNIRTS"
[2] pry(main)> var
=> "GNIRTS"
[3] pry(main)>

Let’s pretend our goal is to take our little string and reverse then upcase it. We then want to store this result in a variable called var. Let’s also pretend we want to see whats happening between the reverse phase and the upcase phase. This is where tap comes in.

We can see our final output is neatly stored in the var variable, and tap hasn’t interfered. But we can also see the state of the string right before the upcase phase.

If you have several methods chained together doing some heavy lifting, this is fantastic for doing some debugging right smack in the middle of them.

This method is definitely more common than the previous but sometimes easily forgotten about in the fury of development. This is just like the classic each method, but it gives you one crucial benefit: an index.

each_with_index not only gives you access to each individual element within an enumerable, but it also gives you the current index number of the element you’re looking at. This can be useful if you might need to jump around or skip elements in an enumerable based on its index.

Let’s say you have an array of data where the second and third elements are some type of unimportant header value that you want to discard. You want to keep the rest of the data but simply skip these values while you iterate over the array. You could do something like this:

[1] pry(main)> arr = ["one", "two", "three", "four", "five"]
[1] pry(main)> arr.each_with_index do |val, index|
[1] pry(main)* next if index.between?(1,2)
[1] pry(main)* <do_something_with_val>
[1] pry(main)* end

Although this is a very iterative way to handle this type of situation, it outlines the functionality of this method and all the unique ways you could leverage it.

If you’ve ever tried your hand at duck typing (props to Malina Tran for the super article) or read any Sandi Metz books, then you’ve likely come into contact with the respond_to? method.

This method returns true if the object responds to the passed method name. This can be a string of symbols which is the name of a method. Check out this quick example below on a string:

[1] pry(main)> "string".respond_to?(:chomp)
=> true

This method provides insight not necessarily into what the object you’re working with is, but what it responds to or quacks like.

Remember, you’re sending messages between objects — you don’t need to know all the intricate details about the object you’re talking to. In fact, you shouldn’t really. Maintaining abstraction is key to agile, extensible interfaces, and this method allows you to maintain more abstraction.

This is a very simple method that operates on strings and essentially removes duplicate characters. If you simply call the squeeze method on a string with multiple of the same characters, you’ll get back a string with the duplicates removed:

[1] pry(main)> "aabbccdd".squeeze
=> "abcd"

You can also pass squeeze — a range of characters in string form that should be operated on — on an argument. This is useful if you only want to remove duplicates of certain characters.

Conclusion

I hope you’ve enjoyed learning about some of these methods and try them out in your own project — or even just play around with them in irb or pry to learn more about how they work,

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Tate Galbraith

Written by

Software Engineer @mixhalo & die-hard Rubyist. Amateur Radio operator with a love for old technology. Tweet at me: https://twitter.com/@Tate_Galbraith

Better Programming

Advice for programmers.

Tate Galbraith

Written by

Software Engineer @mixhalo & die-hard Rubyist. Amateur Radio operator with a love for old technology. Tweet at me: https://twitter.com/@Tate_Galbraith

Better Programming

Advice for programmers.

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