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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:29:13+00:00 2026-05-15T05:29:13+00:00

I frequently write something like this: a_hash[‘x’] ? a_hash[‘x’] += ‘ some more text’

  • 0

I frequently write something like this:

a_hash['x'] ? a_hash['x'] += ' some more text' : a_hash['x'] = 'first text'

There ought to be a better way to do this, but I can’t find it.

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

    There are two ways to create initial values with for a Hash.

    One is to pass a single object in to Hash.new. This works well in many situations, especially if the object is a frozen value, but if the object has internal state, this may have unexpected side-effects. Since the same object is shared between all keys without an assigned value, modifying the internal state for one will show up in all.

    a_hash = Hash.new "initial value"
    a_hash['a'] #=> "initial value"
    # op= methods don't modify internal state (usually), since they assign a new
    # value for the key.
    a_hash['b'] += ' owned by b' #=> "initial value owned by b"
    # other methods, like #<< and #gsub modify the state of the string
    a_hash['c'].gsub!(/initial/, "c's")
    a_hash['d'] << " modified by d"
    a_hash['e'] #=> "c's value modified by d"
    

    Another initialization method is to pass Hash.new a block, which is invoked each time a value is requested for a key that has no value. This allows you to use a distinct value for each key.

    another_hash = Hash.new { "new initial value" }
    another_hash['a'] #=> "new initial value" 
    # op= methods still work as expected
    another_hash['b'] += ' owned by b'
    # however, if you don't assign the modified value, it's lost,
    # since the hash rechecks the block every time an unassigned key's value is asked for
    another_hash['c'] << " owned by c" #=> "new initial value owned by c"
    another_hash['c'] #=> "new initial value"
    

    The block is passed two arguments: the hash being asked for a value, and the key used. This gives you the option of assigning a value for that key, so that the same object will be presented each time a particular key is given.

    yet_another_hash = Hash.new { |hash, key| hash[key] = "#{key}'s initial value" }
    yet_another_hash['a'] #=> "a's initial value"
    yet_another_hash['b'] #=> "b's initial value"
    yet_another_hash['c'].gsub!('initial', 'awesome')
    yet_another_hash['c'] #=> "c's awesome value"
    yet_another_hash #=> { "a" => "a's initial value", "b" => "b's initial value", "c" => "c's awesome value" }
    

    This last method is the one I most often use. It’s also useful for caching the result of an expensive calculation.

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

Sidebar

Related Questions

I have a table that looks something like this (simplified): *ScheduledReports* ReportID StartDate Frequency
I frequently write code changes (PHP) that go along with a schema change. This
I have a map that is frequently read but rarely write to. Some operations
I frequently find myself writing code like this: List<int> list = new List<int> {
I would like to write some custom VBA functions to perform calculations on particular
I realize this sounds like something a malware program would do, so I understand
Why the designers of C# did not allow for something like this? public readonly
I frequently write throwaway code (in a research environment ) - for example to
I frequently write for-loops in bash with the well-known syntax: for i in {1..10}
I find myself very frequently wanting to write reusable strings with parameter placeholders in

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.