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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:24:02+00:00 2026-05-21T18:24:02+00:00

Is there a nice (one line) way of writing a hash in ruby with

  • 0

Is there a nice (one line) way of writing a hash in ruby with some entry only there if a condition is fulfilled? I thought of

{:a => 'a', :b => ('b' if condition)}

But that leaves :b == nil if the condition is not fulfilled. I realize this could be done easily in two lines or so, but it would be much nicer in one line (e.g. when passing the hash to a function).

Am I missing (yet) another one of ruby’s amazing features here? 😉

  • 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-21T18:24:03+00:00Added an answer on May 21, 2026 at 6:24 pm

    UPDATE Ruby 2.4+

    Since ruby 2.4.0, you can use the compact method:

    { a: 'a', b: ('b' if cond) }.compact
    

    Original answer (Ruby 1.9.2)

    You could first create the hash with key => nil for when the condition is not met, and then delete those pairs where the value is nil. For example:

    { :a => 'a', :b => ('b' if cond) }.delete_if{ |k,v| v.nil? }
    

    yields, for cond == true:

    {:b=>"b", :a=>"a"}
    

    and for cond == false

    {:a=>"a"} 
    

    UPDATE for ruby 1.9.3

    This is equivalent – a bit more concise and in ruby 1.9.3 notation:

    { a: 'a', b: ('b' if cond) }.reject{ |k,v| v.nil? }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I assume there is a nice one-line way to say in ruby if mystr
Is there a way to make a HTML textarea element to allow only one
Is there a nice way in the WinAPI to get a path relative to
Is there a nice way in Ext to move elements in DOM? I want
In Spring Source Toolsuite (Eclipse with some Spring tuning) there is nice wizard to
I got some binary files containing integers. Is there some nice Unix command, that
I know there are some pretty nice open-source chess engines with powerful AI, like
Is there a nice linqy way of splitting a FormCollection into a Dictionary<string,string> that
Is there a way to make Resharper 4.5 understand a multi-line TODO or NOTE?
Are there any nice libraries to render text in an image for Java? Java

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.