How to create mutiple hash dynamicaly

Hi i am having two arrays and i have append into a hash like this.Please
help me.

{:name=>"Databases",:url=>"http://www.examplw.com"},
{:name=>"Network management",:url=>"http://www.examplw.com"},
{:name=>"Security",:url=>"http://www.examplw.com"},
{:name=>"Service",:url=>"http://www.examplw.com"}
{:name=>"Storage",:url=>"http://www.examplw.com/"}

On Mar 25, 10:34 am, Nike M. [email protected] wrote:

Hi i am having two arrays and i have append into a hash like this.Please
help me.

{:name=>"Databases",:url=>"http://www.examplw.com"},
{:name=>"Network management",:url=>"http://www.examplw.com"},
{:name=>"Security",:url=>"http://www.examplw.com"},
{:name=>"Service",:url=>"http://www.examplw.com"}
{:name=>"Storage",:url=>"http://www.examplw.com/"}


Posted viahttp://www.ruby-forum.com/.

You need to provide more information before anyone can help you. Your
intentions aren’t clear from this message. What exactly are you trying
to do?

Regards,
Lee

Lee J. wrote:

On Mar 25, 10:34�am, Nike M. [email protected] wrote:

Hi i am having two arrays and i have append into a hash like this.Please
help me.

{:name=>“Databases”,:url=>“http://www.examplw.com”},
{:name=>“Network management”,:url=>“http://www.examplw.com”},
{:name=>“Security”,:url=>“http://www.examplw.com”},
{:name=>“Service”,:url=>“http://www.examplw.com”}
{:name=>“Storage”,:url=>“http://www.examplw.com/”}

Posted viahttp://www.ruby-forum.com/.

You need to provide more information before anyone can help you. Your
intentions aren’t clear from this message. What exactly are you trying
to do?

Regards,
Lee

How to create mutiple hash like this

[{:a=>‘1’,:b=>‘10’}{:a=>‘11’,:b=>‘101’}]

On Thu, Mar 25, 2010 at 12:23 PM, Nike M. [email protected]
wrote:

[{:a=>‘1’,:b=>‘10’}{:a=>‘11’,:b=>‘101’}]

It would be clearer if you specified with a code example the input
data and the expected output (btw, the above is not correct syntax).
But I’ll take a stab:

irb(main):001:0> first_array = [“Databases”, “Network Management”,
“Security”]
=> [“Databases”, “Network Management”, “Security”]
irb(main):002:0> second_array = [“http://databases”, “http://network”,
http://security”]
=> [“http://databases”, “http://network”, “http://security”]
irb(main):004:0> first_array.zip(second_array).inject([]) {|result,
(k,v)| result << {k => v}; result}
=> [{“Databases”=>“http://databases”}, {“Network
Management”=>“http://network”}, {“Security”=>“http://security”}]

Is that what you want?

Jesus.

2010/3/25 Jesús Gabriel y Galán [email protected]:

{:name=>“Storage”,:url=>“http://www.examplw.com/”}
How to create mutiple hash like this
http://security”]
=> [“http://databases”, “http://network”, “http://security”]
irb(main):004:0> first_array.zip(second_array).inject([]) {|result,
(k,v)| result << {k => v}; result}
=> [{“Databases”=>“http://databases”}, {“Network
Management”=>“http://network”}, {“Security”=>“http://security”}]

Sorry, I meant this:

irb(main):005:0> first_array.zip(second_array).inject([]) {|result,
(k,v)| result << {:name => k, :url => v}; result}
=> [{:url=>“http://databases”, :name=>“Databases”},
{:url=>“http://network”, :name=>“Network Management”},
{:url=>“http://security”, :name=>“Security”}]

Jesus.

Something like

a = Array.new

(1…10).each do |x|
a << { :name => x, :value => rand()}
end

gives

[
{:value=>0.275365921616629, :name=>1},
{:value=>0.908041472109624, :name=>2},
{:value=>0.456925255668977, :name=>3},
{:value=>0.960411659941877, :name=>4},
{:value=>0.322421277822909, :name=>5},
{:value=>0.566391285531641, :name=>6},
{:value=>0.658230197869362, :name=>7},
{:value=>0.319688834896862, :name=>8},
{:value=>0.0955639492351847, :name=>9},
{:value=>0.794232897318298, :name=>10}
]

irb(main):001:0> first_array = [“Databases”, “Network Management”, “Security”]
=> [“Databases”, “Network Management”, “Security”]
irb(main):002:0> second_array = [“http://databases”, “http://network”,
http://security”]
=> [“http://databases”, “http://network”, “http://security”]

Sorry for the third post in a row, but this is much better done like
this:

irb(main):006:0> first_array.zip(second_array).map {|(k,v)| {:name =>
k, :url => v}}
=> [{:url=>“http://databases”, :name=>“Databases”},
{:url=>“http://network”, :name=>“Network Management”},
{:url=>“http://security”, :name=>“Security”}]

Jesus.

Thanks for all replies

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs