hash problem

  • Thread starter Antonio Fernández vara
  • Start date
A

Antonio Fernández vara

Hi all,

I'm sure that there is somewhere an explanation on how this works but I
can't find it by myself.

I need to have a hash like this one:

{"name":"Admiral Ackbar"},{"name":"Bail Organa"}

so I need a hash with hashes inside.

If I make

myhash.merge ({"name" => "Bail Organa"})

all I get it's just the last pair, since it's updating it everytime.

How can I have a hash like the first one?

I'm sure someone had this problem before.

Thanks in advance.

Regards,
Antonio
 
P

Peter Hickman

Firstly hash notation is {"name" => "Admiral Ackbar"} and not
{"name":"Admiral Ackbar"} (are you thinking about PHP or Python by
chance?)

To be honest it looks more like you need an Array of hashes.

data = Array.new
data << {"name" => "Admiral Ackbar"}
data << {"name" => "Bail Organa"}

puts data.inspect
=> [{"name"=>"Admiral Ackbar"}, {"name"=>"Bail Organa"}]
 
S

senyi Bojorquez

Hi Peter
You have to do something like this:

hash =3D {}
hash[:name] =3D 'Senyi'
array =3D []
array << hash
hash2 =3D {}
hash2[:name] =3D 'Peter'
array << hash2
=20
=3D> [{:name=3D>"Senyi"}=2C {:name=3D>"Peter"}]=20

I hope it helps you=20
=
Senyi =20


Date: Thu=2C 2 Sep 2010 00:43:12 +0900
From: (e-mail address removed)
Subject: Re: hash problem
To: (e-mail address removed)
=20
Firstly hash notation is {"name" =3D> "Admiral Ackbar"} and not
{"name":"Admiral Ackbar"} (are you thinking about PHP or Python by
chance?)
=20
To be honest it looks more like you need an Array of hashes.
=20
data =3D Array.new
data << {"name" =3D> "Admiral Ackbar"}
data << {"name" =3D> "Bail Organa"}
=20
puts data.inspect
=3D> [{"name"=3D>"Admiral Ackbar"}=2C {"name"=3D>"Bail Organa"}]
=20
=
 
B

Brian Candler

Antonio said:
I need to have a hash like this one:

{"name":"Admiral Ackbar"},{"name":"Bail Organa"}

Why a hash? If it only contains names, then why not

names = ["Admiral Ackbar", "Bail Organa"]

Or so you want a hash with multiple values for one key:
{"name"=>["Admiral Ackbar","Bail Organa"]}

myhash = Hash.new { |h,k| h[k] = [] }

myhash["name"] << "Admiral Ackbar"
myhash["name"] << "Bail Organa"

Otherwise, as others have suggested, an array of hashes:

[{"name"=>"Admiral Ackbar","age"=>30},
{"name"=>"Bail Organa","age"=>65}]

Or choose some attribute as a key, like id

{ 1 => {"name"=>"Admiral Ackbar","age"=>30},
2 => {"name"=>"Bail Organa","age"=>65}}
 
F

F. Senault

Le 01 septembre à 17:43, Peter Hickman a écrit :
data = Array.new
data << {"name" => "Admiral Ackbar"}
data << {"name" => "Bail Organa"}

C'mon, where's the fun in that ? :)
require "ostruct" => true
a = [] => []
a << OpenStruct.new:)name => "Admiral Ackbar", :age => 30)
=> [# said:
a << OpenStruct.new:)name => "Bail Organa", :age => 65)
a[0].name => "Admiral Ackbar"
a[1].age => 65
a.find { |e| e.age > 50 }
=> #<OpenStruct name="Bail Organa", age=65>

Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top