Home > Software design >  Updated Ruby array of hash with new values from another hash
Updated Ruby array of hash with new values from another hash

Time:12-08

john = {"111":{"placeId":"333"} , "22":{"placeId":"22"}}
adam = [{"id": "333","doc": "30"},{"id": "22","doc": "31"}]
sams_collection = [{"some_id": "30"}, {"some_id": "31"}]
 
adam.each do | adam_data |
  john.each_key do | john_data_key |
    if (adam_data['id'] == john['john_data_key']['placeId'])
      john['john_data_key']['org'][adam_data['id']] = sams_collection
    end
  end
end      

I'm currently running into an error:

undefined method `[]' for nil:NilClass (NoMethodError))

I'm expecting john to update as below.

john = {"111":{"placeId":"333", "org" : {"333" : ["30", "31"]} } , "22":{"placeId":"22"}}

CodePudding user response:

There's a couple of issues here.

First of all, because you are using the JSON format for defining your hashes:

{ a: b, c: d }

As opposed to hashrocket style (with =>):

{ a => b, c => d }

You are using the symbols for 'a' and 'c' instead of the string. Specifically, you need to lookup and understand that in ruby, :a is a symbol, and "a" is a string.

You can see this if you do something like:

puts adam.inspect
# returns [{:id=>"333", :doc=>"30"}, {:id=>"22", :doc=>"31"}]

If you wanted to use strings, then you would need to use the "key" => val format instead.

It gets more confusing because you are using numbers as strings, which gives a whiff of code smell - do you really want to store/ref these numbers as strings?

But to confuse it more, you have keys as numbers... except they are strings... except they are actually symbols.

Take a look at john:

john = {"111":{"placeId":"333"} , "22":{"placeId":"22"}}

It looks like you're referencing the first hash using the key 111, but it seems to be in strings, except that due to the a: b it's becoming the symbol for the string for the number, which looks like:

:"111"

So that's a world of confusing.

But then on top of all of this, you have:

john['john_data_key']

That means you want to access the john hash using the key "john_data_key" - not the value in the variable john_data_key. The latter would be:

john[john_data_key]

So you have a number of issues here.

I would recommend looking at walking through this with a debugger, or at least doing some puts my_variable.inspect so you can see what is actually in these data structures you are creating.

Then reconsider how you are structuring your data and how you want to access it, and then this problem will become much easier to manage.

CodePudding user response:

john = {"111":{"placeId":"333"} , "22":{"placeId":"22"}}

adam = [{"id": "333","doc": "30"},{"id": "22","doc": "31"}]
 
sams_collection = [{"some_id": "30"}, {"some_id": "31"}]
 
   adam.each do | adam_data |
      john.each_key do | john_data_key |
        if (adam_data[:id] == john[john_data_key][:placeId])
          some = { adam_data[:doc] => sams_collection}
          john[john_data_key][:org] = some
        end
    end
  end 
  
p john.to_s
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Thanks for the suggestions --> Here is the working code with the same data :|

"{:\"111\"=>{:placeId=>\"333\", :org=>{\"30\"=>[{:some_id=>\"30\"}, {:some_id=>\"31\"}]}}, :\"22\"=>{:placeId=>\"22\", :org=>{\"31\"=>[{:some_id=>\"30\"}, {:some_id=>\"31\"}]}}}"
  • Related