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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:58:04+00:00 2026-05-15T12:58:04+00:00

I am migrating my PHP code to Ruby and at some point I need

  • 0

I am migrating my PHP code to Ruby and at some point I need to update hash elements inside of a loop. For example:

compositions.each_pair do |element,params|
  params['composition'].each_pair do |item,data| 
    data['af'] /= params['af sum']
    data['mf'] /= params['mass']
  end
end

I could make it using item indexes, but it will be ugly. Is there a nice way to link the loop variables to the corresponding hash items? In PHP I would write &$params and &$data in the corresponding loops. Or may be a better suggestion?


Update

Two tests to demonstrate the problem. The first one:

a={'a'=>1, 'b'=>2, 'c'=>3}

a.each_pair do |k,v|
  v += 1
end
p a # =>  {"a"=>1, "b"=>2, "c"=>3}

and the second

a.each_pair do |k,v|
  a[k] += 1
end
p a # =>  {"a"=>2, "b"=>3, "c"=>4}

Update2

Thanks to Mladen (see below), I have understood the difference between these two cases. However, I still have a question: how to update data item (not just its own items)? Let’s say we add

data = data['af'] + data['mf']

to the inner loop.

  • 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-15T12:58:05+00:00Added an answer on May 15, 2026 at 12:58 pm

    It seems that the code from the question works OK:

    compositions = {1 => {'af sum' => 100.0, 'mass' => 200.0, 'composition' => {1 => {'af' => 5.0, 'mf' => 6.0}, 2  => {'af' => 7.0, 'mf' => 8.0}, 3  => {'af' => 9.0, 'mf' => 16.0}}}}
    #=> {1=>{"af sum"=>100.0, "mass"=>200.0, "composition"=>{1=>{"af"=>5.0, "mf"=>6.0}, 2=>{"af"=>7.0, "mf"=>8.0}, 3=>{"af"=>9.0, "mf"=>16.0}}}}
    compositions.each_pair do |element,params|
      params['composition'].each_pair do |item,data| 
        data['af'] /= params['af sum']
        data['mf'] /= params['mass']
      end
    end
    #=> {1=>{"af sum"=>100.0, "mass"=>200.0, "composition"=>{1=>{"af"=>0.05, "mf"=>0.03}, 2=>{"af"=>0.07, "mf"=>0.04}, 3=>{"af"=>0.09, "mf"=>0.08}}}}
    

    But the example structure is pretty much guessed from the code, OP should post an example he is working with so we could actually make some progress.

    Edit:

    When you perform + method on an integer, a new object is returned as a result. That object is assigned to v in your first example, but not assigned back to the hash, so it is not preserved. In your second example, you assign it back to the hash.

    However, if you work with mutating methods, you alter the very objects in-place, so then you don’t need to reassign them to the hash. For example:

    {1 => 'foo', 2 => 'bar'}.each{|k,v| v.swapcase!}
    #=> {1=>"FOO", 2=>"BAR"}
    

    but

    {1 => 'foo', 2 => 'bar'}.each{|k,v| v = 'baz'}
    #=> {1=>"foo", 2=>"bar"}
    

    Edit 2:

    I think you have problem understanding what k and v in the examples are. They are just local variables in the block, they start by referencing whatever is in the hash, but what do they reference can be changed during course of a block just like with any other variable. That change won’t reflect to the hash. You need to alter the very object, not the reference to it, to actually change it.

    Take a look at this, even simpler example, no hashes, blocks mumbo-jumbo:

    a = 5
    b = a
    b += 1
    a # => 5
    b # => 6
    

    It’s as simple as that.

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

Sidebar

Ask A Question

Stats

  • Questions 470k
  • Answers 470k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Console.WriteLine("{0:N2}", ((double)n) / 1450); May 16, 2026 at 3:01 am
  • Editorial Team
    Editorial Team added an answer Yes, you need to implement a Converter. It's not that… May 16, 2026 at 3:01 am
  • Editorial Team
    Editorial Team added an answer There is no standard property for that. And for good… May 16, 2026 at 3:01 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.