Hashes in array?

Is this possible? If so, how…???

array = [ {:key1 => “value1”}, {:key2 =>“value2”} ]

to retrieve them you can do something like this:

array.each do |hash|
hash.each do |key, value|
puts key
puts value
end
end

a = [ { :one => 1 } ]

Hi –

On Wed, 27 Sep 2006, bluengreen wrote:

Is this possible? If so, how…???

[ { 1 => 2, 3 => 4 }, { “apple” => “red”, “banana” => “yellow” } ]

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Guest wrote:

array = [ {:key1 => “value1”}, {:key2 =>“value2”} ]

to retrieve them you can do something like this:

array.each do |hash|
hash.each do |key, value|
puts key
puts value
end
end

What’s the advantage of using :key as opposed to “key”? I’ve seen it
advertised as a ‘best practice’ on the Rails Wiki, but it didn’t go into
the rationale.

Thanks,
Nelson

:key is a Symbol literal which is always the same object in memory.
“key” is a String literal which will create a new object every time you
say “key”. So, there are performance gains, but more important for me
are the semantics of a symbol (think of it like an identifier or a
name) and the simple fact that you type and look at less code.

Symbols are somewhat tricky to really understand, but I think some of
the details are no important once you get used to common usage. They
will eventually be a type of String with Ruby 2, but for now, though
they seem similar, :key.is_a?(String) returns a most certain false.

Thanks for all the reply’s. Unfortunately i was leaving work when I
wrote the thread and I was a little brain dead from trying to solve a
problem. I meant to say into sessions not arrays.

I’m trying to save hashes into the session and I keep getting 500
errors and TypeErrors.

I’m trying to connect to a LDAP directory and save the entry from a
search into session for caching from page to page with looking it up
again.

This is how the applications we have at my work have been written and
I want to follow the same methodology. But I keep getting the
errors. I know it’s probably related to the object/hash being passed
can not be marshaled. However, my Ruby skills are weak at best. So
that doesn’t mean squat to me. I pass it to a temp var, I’ve used
to_s and that works but I can’t get me stuff back out like I want.
It’s just one big string. I pass the hash using to_hash and it gives
the same errors as if I passed the object directly.

I dunno. Do you guys know?

thanks,
phill

Oops I meant sessions

a = Array.new
=> []

h1 = Hash.new
=> {}

h2 = Hash.new
=> {}

h3 = Hash.new
=> {}

h1[:stuff] = ‘marshmallows’
=> “marshmallows”

h2[:stuff] = ‘graham crackers’
=> “graham crackers”

h3[:stuff] = ‘hershey bar’
=> “hershey bar”

a << h1
=> [{:stuff=>“marshmallows”}]

a << h2
=> [{:stuff=>“marshmallows”}, {:stuff=>“grahm crackers”}]

a << h3
=> [{:stuff=>“marshmallows”}, {:stuff=>“grahm crackers”},
{:stuff=>“hershey bar”}]

bluengreen ïèøåò:

Oops I meant sessions

bluengreen wrote:

Is this possible? If so, how…???

Why not ?

session[:blah] = { :apple => ‘red’ }
session[:blah][:foo] = ‘bar’

yeah read my follow up…hash in arrays

it’s been a pain…mainly because I don’t know Ruby enough …

you can always examine the contents of the hash in the development error
screen and see the hash arrays (key => ‘value’) and see what you need to
see.

or

create a simple view template that has
<%= debug session %>

which will display your session hash in nice formed statements

Craig

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