DEV Community

Michael Herold
Michael Herold

Posted on • Originally published at michaeljherold.com on

2 2

Using the double-splat operator in Ruby

The double-splat operator is one of my favorite additions in Ruby 2.0. For some reason, the two little asterisks together make me happier than an optional Hash parameter in a method. Even so, outside of method definitions, I didn’t realize there was a use for the double-splat operator until I dug into a bug in Hashie. I liked the result enough that I wanted to take a quick minute and share what I found.

Related behavior in JavaScript

If you write any modern JavaScript, you might have seen the following pattern:

function todoApp(state = initialState, action) {
  switch (action.type) {
    case SET_VISIBILITY_FILTER:
      return {
        ...state,
        visibilityFilter: action.filter
      };
    default:
      return state;
  }
}

Enter fullscreen mode Exit fullscreen mode

That ... spread operator is really nice for creating new objects from others without modifying the originals. I really liked this when I built my first Redux-based JavaScript application because it removes a lot of the ceremony around Object.assign.

Sometimes you don’t yet have enough information to create an object. When that happens Hash is a great proxy for an object until you understand your domain a little better. I wanted the same behavior in Ruby for building up Hashes in these cases. For a while, I relied on Hash#merge, but then I saw this bug on Hashie.

Double-splat operator

To solve this problem, you can use the double-splat operator in a different way. Check out the following code:

def todo_app(state = initial_state, action)
  case action.type
  when SET_VISIBILITY_FILTER then { **state, visibility_filter: action.filter }
  else state
  end
end

Enter fullscreen mode Exit fullscreen mode

This snippet works exactly the same way as the JavaScript version! I think that’s a really elegant answer to my original question.

Interestingly, if you compile down the instruction sequence for this code, you will see that it uses the C version of Hash#merge under the covers. I like that I’m getting the semantics of state.merge(visibility_filter: action.filter), but can express it in a way that my JavaScript-writing friends can easily understand.

If there’s one way to do something in Ruby, there’s probably at least one more. I think this is one of the reasons many people like Ruby: they can express their thoughts in the manner that is easiest for them.

That’s all I have for you today. Did you know about the double-splat operator in this case?

The post Using the double-splat operator in Ruby appeared first on Michael Herold.

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Sentry image

Make it make sense

Make sense of fixing your code with straight-forward application monitoring.

Start debugging →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay