0
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"}}

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Great answer. The two ways of writing hash literals are commonly called hashrocket (=>) and JSON style.
Yea, I was trying to remember the name for the second style, I'll update the answer.
0

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

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\"}]}}}"

3 Comments

I would argue that there are still problems with this solution. You are using quotes around symbols which is obfuscating what they actually are. Please lookup symbols vs strings in ruby (knowing the difference is very useful, and symbols can be far more efficient than strings for indexing things where the names are constant).
(And you'll notice you need the quotes around the numeric symbols you have - but that's partly because numeric symbols are rarely a good idea and demonstrate a likely misunderstanding of how the symbols work.) So I would reconsider your solution. You probably want to use symbols where symbols make sense and numbers where numbers make sense. For example, have the john hash index by actual numbers, not symbols. And have id be a symbol, and it points to the number 333 or 22 or whatever, instead of the string 333/22/...
Thanks for the help, I understand what you are trying to explain

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.