Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8066829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:06:55+00:00 2026-06-05T12:06:55+00:00

How do I conditionally add a hash to an *args array in Rails? I

  • 0

How do I conditionally add a hash to an *args array in Rails? I don’t want to stomp on the original value if one exists.

For instance, I have an method that receives an array:

def foo(*args)
  # I want to insert {style: 'bar'} into args but only if !style.present?
  bar(*args)                              # do some other stuff 
end

I’ve started using the extract_options and reverse_merge methods provided by rails:

def foo(*args)
  options = args.extract_options!         # remove the option hashes
  options.reverse_merge!  {style: 'bar'}  # modify them
  args << options                         # put them back into the array
  bar(*args)                              # do some other stuff 
end

It works but seems verbose and not very ruby-ish. I feel like I’ve missed something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:06:56+00:00Added an answer on June 5, 2026 at 12:06 pm

    Yes, #extract_options! is the way to do it in Rails. If you want to be more elegant, you’ll have to alias it, or find your own way of handling this need, or search for gem by someone who has already done it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I conditionally add the style 'color:red' to the .CurrentDifference if the value
How can I conditionally add 'b' => 'xyz' in the array below, in the
I want to know how to Add a UserControl Conditionally to a Repeater Control.
I want to conditionally add an attribute using jQuery: var $this = this .append(...)
I know I can conditionally add an id value via <select id=@(aBool ? @someId:
I can add a Conditional statement to a breakpoint, for instance arg0.startsWith(something) but i'd
I want to conditionally output HTML to generate a page, so what's the easiest
I want to conditionally navigate to some page. If some condition is true i
I am conditionally using two different url patterns, mean on one URL I am
I'm wondering if it's possible to conditionally add a parameter in a call to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.