f



Best way to create hash of hashes

Hi

What is the best way when collecting data to create a has of hashes?

So say this was my expected result.

{ Id:123 { id:1, name:John},{id:2, name: Sarah}}

So that I am saying Sarah and John are members of group id: 123.

Thanks

Sayth
0
Sayth
6/25/2016 1:50:35 AM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

5 Replies
399 Views

Similar Articles

[PageSpeed] 3

On 25.06.2016 03:50, Sayth Renshaw wrote:

> What is the best way when collecting data to create a has of hashes?
>
> So say this was my expected result.
>
> { Id:123 { id:1, name:John},{id:2, name: Sarah}}

This is not a valid representation of a Hash.  Which of these do you want?

irb(main):001:0> { 'Id:123' => [{id:1, name:'John'}, {id:2, name:'Sarah'}]}
=> {"Id:123"=>[{:id=>1, :name=>"John"}, {:id=>2, :name=>"Sarah"}]}

irb(main):002:0> { Id:123, members: [{id:1, name:'John'}, {id:2, 
name:'Sarah'}]}
=> {:Id=>123, :members=>[{:id=>1, :name=>"John"}, {:id=>2, :name=>"Sarah"}]}

....

> So that I am saying Sarah and John are members of group id: 123.

That depends on your input and how you access it.

Kind regards

	robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
0
Robert
6/25/2016 10:16:12 AM
> This is not a valid representation of a Hash.  Which of these do you want?
> 
> irb(main):001:0> { 'Id:123' => [{id:1, name:'John'}, {id:2, name:'Sarah'}]}
> => {"Id:123"=>[{:id=>1, :name=>"John"}, {:id=>2, :name=>"Sarah"}]}
> 
> irb(main):002:0> { Id:123, members: [{id:1, name:'John'}, {id:2, 
> name:'Sarah'}]}
> => {:Id=>123, :members=>[{:id=>1, :name=>"John"}, {:id=>2, :name=>"Sarah"}]}
> 
> ...
> 
> > So that I am saying Sarah and John are members of group id: 123.
> 
> That depends on your input and how you access it.
> 
> Kind regards
> 
> 	robert
> 
> 
> -- 
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/

Is there a particular benefit of one over the other? I want to be able to keep the relation between ids and descendant nodes and then put this into a database, is one more suitable than the other?

Thanks

Sayth
0
Sayth
6/25/2016 10:47:12 AM
On 25.06.2016 12:47, Sayth Renshaw wrote:

> Is there a particular benefit of one over the other? I want to be
> able  to keep the relation between ids and descendant nodes and then put this
> into a database, is one more suitable than the other?

Of course.  But it depends on the usage.  You need to make up your mind 
how you want to go about things.  Since we do not know what database you 
want to put that into, how the schema looks (if it has one), what the 
source of data is and all the other circumstances, the only one who can 
realistically answer that question at the current point in time is you.

Kind regards

	robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
0
Robert
6/25/2016 12:19:25 PM
Hi

The data I will be collecting from XML has a snowflake schema based around, though there are changes I want to make from first version. 

At this stage i just want to ensure I maintain the relationship of each person and id to the event id and will create some json files to play with charts and display before I parse all files and push to database. 

Cheers

Sayth 
0
Sayth
6/25/2016 3:27:31 PM
On 25.06.2016 17:27, Sayth Renshaw wrote:

> The data I will be collecting from XML has a snowflake schema based
> around, though there are changes I want to make from first version.

I am not sure how a snowflake schema looks in XML.  Or is that the 
target DB?

> At this stage i just want to ensure I maintain the relationship of
> each person and id to the event id and will create some json files to
> play with charts and display before I parse all files and push to
> database.

Wouldn't it be sufficient then to store the event id in every person? 
And what do you need JSON for when you read from XML and store in a 
(presumably) relational DB?

Cheers

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
0
Robert
6/25/2016 10:26:27 PM
Reply: