Associative arrays (or hash) php to ruby?

A more-or-less direct rewrite from your PHP: albums = {} songs = { :title => 'Title 1', :artist => 'Artist 1', :album => 'Album 1' }, { :title => 'Title 2', :artist => 'Artist 2', :album => 'Album 2' }, { :title => 'Title 3', :artist => 'Artist 1', :album => 'Album 1' }, { :title => 'Title 4', :artist => 'Artist 2', :album => 'Album 1' }, { :title => 'Title 5', :artist => 'Artist 3', :album => 'Album 1' } songs. Each do |song| album = albumssong:album ||= {} album:title = song:title (album:songs ||= ). Push(song) end puts albums.

Inspect Easier would be the one-liner: albums = songs. Group_by { |song| song:album } Arrays have only integer keys, and allocate all of them up to the highest one (so if you assign foo100 = 1 there will be locations for keys 0-99 also occupying memory (with nil value); you note array values with square brackets: 1, 2, 3 is a 3-element array is empty Hashes are enclosed in curly braces {} and can have pretty much anything as keys. However, blocks can also be written with braces, so take care not to confuse the two.

A more-or-less direct rewrite from your PHP: albums = {} songs = { :title => 'Title 1', :artist => 'Artist 1', :album => 'Album 1' }, { :title => 'Title 2', :artist => 'Artist 2', :album => 'Album 2' }, { :title => 'Title 3', :artist => 'Artist 1', :album => 'Album 1' }, { :title => 'Title 4', :artist => 'Artist 2', :album => 'Album 1' }, { :title => 'Title 5', :artist => 'Artist 3', :album => 'Album 1' } songs. Each do |song| album = albumssong:album ||= {} album:title = song:title (album:songs ||= ). Push(song) end puts albums.

Inspect Easier would be the one-liner: albums = songs. Group_by { |song| song:album } Arrays have only integer keys, and allocate all of them up to the highest one (so if you assign foo100 = 1, there will be locations for keys 0-99 also occupying memory (with nil value); you note array values with square brackets: 1, 2, 3 is a 3-element array, is empty. Hashes are enclosed in curly braces {}, and can have pretty much anything as keys.

However, blocks can also be written with braces, so take care not to confuse the two.

You should be able to use a Ruby Hash exactly like a PHP associative array.

As you mentioned in the title of your question, associative arrays are usually referred to as hashes in Ruby. The syntax is slightly different from arrays. # Normal array normArray = "SomeSong", "SomeGuy" anotherNormArray = Array.

New # Literal hash literalHash = { "Title" => "SomeSong", "Artist" => "SomeGuy" } # Declared beforehand declaredHash = Hash. New declaredHash"Songs" = literalHash.

You can do it in one line songs = {'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'}, {'title' => 'Title 2', 'artist' => 'Artist 2', 'album' => 'Album 2'}, {'title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1'}, {'title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1'}, {'title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1'} songs. Group_by{|h| h:album}. Inject({}){|h,(album,s)| halbum = {:title => album, :songs => s }; h } #=> {"Album 1"=>{:songs=>{:artist=>"Artist 1", :album=>"Album 1", :title=>"Title 1"}, {:artist=>"Artist 1", :album=>"Album 1", :title=>"Title 3"}, {:artist=>"Artist 2", :album=>"Album 1", :title=>"Title 4"}, {:artist=>"Artist 3", :album=>"Album 1", :title=>"Title 5"}, :title=>"Album 1"}, "Album 2"=>{:songs=>{:artist=>"Artist 2", :album=>"Album 2", :title=>"Title 2"}, :title=>"Album 2"}} Only I don't understand why do you use Album title twice: as a hash key and as a title param?

You should use only once, I think :).

Nevermind that, it was just an example. The first that came to my mind :) – keepitterron Apr 25 at 22:37.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions