RubyGuides
Share this post!

7 Little-Known Ruby Methods To Help You Write Better Code

Did you know that using the right Ruby method can save you a lot of work?

The more methods you are familiar with the faster you can produce working code & the better this code will be (both in performance & quality).

That’s why today I want to introduce 7 interesting methods to you that you may not have seen before.

Integer#digits Method (Ruby 2.4)

This is a new method introduced in Ruby 2.4 & it’s very useful if you want to work with the individual digits of an Integer.

Note: In case you are not up to speed on Ruby 2.4 changes, Fixnum & Bignum are now deprecated & both merged into Integer.

You may think that using array-like indexing may work:

But what you get are not the digits, but the binary bits that make up this number.

So pre-digits method the typical solution was to convert the number into a string, then break that string into an array of characters using the chars method, and finally convert every string back into an integer.

Example:

But in a post-digits method world, you can simply do this:

Much better, isn’t it?

Video:

The Tap Method

Sometimes you want to create an object, call some methods on it & return that same object.

You would have to do something like this:

The problem is that we have to deal with this temporary variable.

Here’s where the tap method comes to the rescue!

With tap the last example becomes this:

Notice how the temporary variable is gone, which is a good thing 🙂

Array#values_at

If you want to get multiple non-sequential values from an array or a hash you could do this:

Or you could use the values_at method:

This also works with a hash:

Hash#transform_values (Ruby 2.4)

Let’s say you have a hash & want to change all the values for some reason.

Sort of what you would do with Array#map.

The problem with map is that you will get an array-of-arrays (multi-dimensional array) instead of a hash. Also there is no Hash#map!.

So one solution could be this:

In this example we multiply each value times two.

Which results in:

Since Ruby 2.4 there is a better way to do this:

This is another of those methods coming from Rails, which is now available outside the framework 🙂

Kernel#itself (Ruby 2.2)

If you told me that this method does not look very interesting at first I would agree with you.

But the reason is that you need to see some good examples.

So let’s say we have an array with repeated words & we want to count them…

While there are many ways to do this, I think this is a great case for testing out our new friend: Kernel#itself.

Example:

Notice how I also combined this with Hash#transform_values!

If transform_values is not available you can do this:

Video:

Note: The itself method shows up under the Object class in the documentation, but the method is defined on the Kernel module.

Array#count

Sometimes you just need to count something.

Instead of using the typical counter + loop you can use the count method!

Example:

The array version of this method also takes a block, so you can do more complex counting.

Video:

Enumerable#cycle

Do you want to repeat some pattern? Or maybe you need an easy on/off toggle?

Then Enumerable#cycle might be what you are looking for!

Example:

But my favorite use for cycle is to create a “switch” or “toggle” object.

The nice thing about this is that you don’t need to check the current value of the switch to update it manually.

You can just call next & Ruby will know exactly what to do 🙂

Summary

You have learned about some new useful methods: Integer#digits, Kernel#tap, Array#values_at, Hash#transform_values, Kernel#itself, Array#count, Enumerable#cycle.

If you already knew about these great! If not I hope you find them useful 🙂

Enjoyed this article? Then share it with your friends so they can enjoy it too!

14 comments
Serguei says a few months ago

Thank you sharing, Jesus. There is a typo in Hash#map section: “soultion” instead of “solution”.

    Jesus Castello says a few months ago

    Thanks for reading Serguei! Also thanks for reporting the typo, it’s fixed now 🙂

Ari says a few months ago

That was pretty helpful. I knew couple of methods and have used ’em in the past, but some of ’em really looks interesting. Cant wait to play with ’em. Thank you Jesus!

    Jesus Castello says a few months ago

    I’m glad you found this helpful! Thanks for reading 🙂

Dominic says a few months ago

This is really cool, it’s great to still learn new things about Ruby even after using it for so long.

Jordan says a few months ago

Hey mate,

For what it’s worth, ActiveRecord gives you tap-like functionality out of the box, so where you’ve done:

User.new.tap { |user| user.name = "John" }

The following also works:

User.new { |user| user.name = "John" }

    Jesus Castello says a few months ago

    Thanks for sharing 🙂

    Pedro Coutinho says a few months ago

    Or even:

    User.new(name: ‘john’)

Stefan says a few months ago

You usually want the digits in the same order, so it should be 123.to_s.chars.map(&:to_i) vs. 123.digits.reverse.

    Michael Kohl says a few months ago

    The neat thing about the way the digits are arranges is that their array indexes correlated with the power of ten they represent. Or in code:


    [3,2,1].each_with_index.reduce(0) { |sum, (n, i)| sum + n * 10 ** i }
    #=> 123

Nick says a few months ago

I liked that part with the itself method! Thanks Jesus!

    Jesus Castello says a few months ago

    Thanks Nick for reading & leaving a comment 🙂

Vikrant Patel says a few months ago

Very nicely explained article. Thank you for sharing.

    Jesus Castello says a few months ago

    Thanks for reading! 🙂

Comments are closed