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 6882283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:14:03+00:00 2026-05-27T05:14:03+00:00

Is there a short hand or best practice for assigning things to a hash

  • 0

Is there a short hand or best practice for assigning things to a hash when they are nil in ruby? For example, my problem is that I am using another hash to build this and if something in it is nil, it assigns nil to that key, rather than just leaving it alone. I understand why this happens so my solution was:

hash1[:key] = hash2[:key] unless hash2[:key].nil?

Because I cannot have a value in the has where the key actually points to nil. (I would rather have an empty hash than one that has {:key => nil}, that can’t happen)

My question would be is there a better way to do this? I don’t want to do a delete_if at the end of the assignments.

  • 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-05-27T05:14:03+00:00Added an answer on May 27, 2026 at 5:14 am

    a little bit shorter if you negate the “unless” statement

    hash1[:key] = hash2[:key] if hash2[:key]   # same as   if ! hash2[:key].nil?
    

    you could also do the comparison in a && statement as suggested in other answers by Michael or Marc-Andre

    It’s really up to you, what you feel is most readable for you. By design, there are always multiple ways in Ruby to solve a problem.

    You could also modify the hash2 :

    hash1 = hash2.reject{|k,v| v.nil?}
    
    hash2.reject!{|k,v| v.nil?}   # even shorter, if in-place editing of hash2
    

    this would remove key/value pairs :key => nil from hash2 (in place, if you use reject! )

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

Sidebar

Related Questions

Long story short, I have two regex patterns. One pattern matches things that I
Is there any way possible short of hand-coding the Reference.cs file, to get Visual
It appears that if x is almost like short-hand for the longer if x
I'm interested in creating a sort of hand-off authentication method, where there's a client
Is there any shorthand way of defining and using generic definitions without having to
In my code, is there a shorthand that I can use to assign a
Is there a short way to check/get for least significant bit in a 32-bit
In short: Is there any known protocol for remote process management? I have a
When defining a new class within a project what is the correct/best practice for
i have a problem in which i am hoping to find the best and

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.