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

  • SEARCH
  • Home
  • 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 9248685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:59:55+00:00 2026-06-18T09:59:55+00:00

I need to call a function which its params is a hash. For its

  • 0

I need to call a function which its params is a hash. For its params I have a hash, and an attribute that I need to merge. To do so, I use the following, which works correctly:

paramsHash={:att1=> "1", :att2=>"2"} #this is obtained from a function
result=MyClass.where({:att0=> "0"}.merge(paramsHash))

As said, this works, no problem there. My question is, is there a nice ruby fancy way to to this? Something like

paramsHash={:att1=> "1", :att2=>"2"} #this is obtained from a function
result=MyClass.where(:att0=> "0", paramsHash.as_params)

Thanks

  • 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-18T09:59:57+00:00Added an answer on June 18, 2026 at 9:59 am

    There is no fancier way to do this than merge, I just would write it the other way around so you can relieve the curly braces:

    result = MyClass.where(params_hash.merge(att0: "0"))
    

    This is the fanciest way I can think of writing your code. It does however change the way the hashes get merged, which makes no difference in the code you presented in your question, but may make a difference if the same key is present in both hashes.

    Other things to make your ruby fancier:

    • It is common to use underlined notation and not camelCase for local variables in ruby, so params_hash and not paramsHash.
    • Spaces for between the assignment operator, the variable getting assigned and the assignment are common: result = 'this' and not result='this'
    • Same with the key-value pairs in Hashes: {:this => 'is a hash'} and not {:this=>'is a hash'} in ruby 1.9., you can even do {this: 'is a hash'}, which is ruby 1.9 notation for symbols as hash keys.
    • Some rubyists like to relieve the optional braces, you can do that too if you like:

      result = MyClass.where params_hash.merge(att0: "0")

      or

      result = MyClass.where(params_hash.merge att0: "0")

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

Sidebar

Related Questions

I have a javascript function from which I need to call a controller action
I have develop this function which uses recursion to call itself. I need to
In my Application I need to call a function(Which update the valuesof textviews),I need
I need to call a function in the following fashion: var f = 'fadeOut';
I have js file where i need to call the function defined in the
I have a GridView with textboxes in its cells. I need to call a
Let us say I have a void function which just have NSLog(@Call me); in
I have a class named DataBoundObject which is quite big. And works good. Its
I have a scenario where I need to call a Java method which sets
I have been trying to use the row count function which is part of

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.