"2", "Field1"=>"National Life Group","DateCreated"=>"2010-07-30 ...">

Accessing Ruby Array Elements

I have an array that looks like this.

[{"EntryId"=>"2", "Field1"=>"National Life Group","DateCreated"=>"2010-07-30 11:00:14", "CreatedBy"=>"tristanoneil"}, {"EntryId"=>"3", "Field1"=>"Barton Golf Club", "DateCreated"=>"2010-07-30 11:11:20", "CreatedBy"=>"public"}, {"EntryId"=>"4", "Field1"=>"PP&D Brochure Distribution", "DateCreated"=>"2010-07-30 11:11:20", "CreatedBy"=>"public"}, {"EntryId"=>"5", "Field1"=>"Prime Renovation Group, DreamMaker Bath & Kitchen", "DateCreated"=>"2010-07-30 11:11:21", "CreatedBy"=>"public"} ] 

How would I start repeating this array, so I can specify which field I want to print and get the value, so I could do something like this.

 puts EntryId.value 
+7
ruby
source share
3 answers

Having curly braces and hashes ( => ) means that you are dealing with a Ruby Hash, not an array.

Fortunately, extracting a value (the thing to the right of the hash key) associated with any one key (the thing to the left of the hash key) is a piece of the hash pie: all you have to do is use [] .

 entry = { "EntryId" => "2", "Field1" => "National Life Group", ... } entry["EntryId"] # returns "2" 

Here is the documentation for Hash: http://ruby-doc.org/core/classes/Hash.html

+7
source share

This looks like an array of hashes. Suppose this is stored in a variable as follows:

 data = [{"EntryId"=>"2", "Field1"=>"National Life Group"}, {"EntryId"=>"3", "Field1"=>"Barton Golf Club"}, {"EntryId"=>"4", "Field1"=>"PP&D Brochure Distribution"} ] 

Access to individual elements of the array can be obtained using the index in square brackets. The values โ€‹โ€‹of the hashes can be obtained using the key in square brackets. For example, to get the value "Field1" of the second element of the array that you would use:

 data[1]["Field1"] 

You can easily iterate over an array using the methods defined in Enum mixin .

If you want to process the array, you can use each: this code will print the value of the input identifier for each element in your array.

 data.each{|entry| puts entry["EntryId"]} 

This data does not need to be stored in a variable for operation. You can simply access the anonymous array directly using these methods:

For example, this will return an array of strings. Where each element from the returned array is a formatted version of the corresponding element in the original array.

 [{"EntryId"=>"2", "Field1"=>"National Life Group"}, {"EntryId"=>"3", "Field1"=>"Barton Golf Club"}, {"EntryId"=>"4", "Field1"=>"PP&D Brochure Distribution"} ].map{|e| "EntryId: #{e["EntryId"]}\t Company Name: #{e["Field1"]}"} 
+7
source share

Whenever I see multidimensional arrays, I wonder if it could not be simpler and more understandable with a small class or structure that looks like a lightweight class.

eg.

 # define the struct Thing = Struct.new( "Thing", :entry_id, :field_1, :date_created , :created_by) directory = Hash.new # create a hash to hold your things keyed by entry_id # create your things and add them to the hash thing = Thing.new(2, "National Life Group", "2010-07-30 11:00:14", "tristanoneil" ) directory[thing.entry_id] = thing thing = Thing.new(3, "Barton Golf Club", "2010-07-30 11:00:14", "public" ) directory[thing.entry_id] = thing thing = Thing.new(4, "PP&D Brochure Distribution", "2010-07-30 11:00:14", "public" ) directory[thing.entry_id] = thing thing = Thing.new(5, "Prime Renovation Group, DreamMaker Bath & Kitchen", "2010-07-30 11:00:14", "public" ) directory[thing.entry_id] = thing # then retrieve what you want from the hash my_thing = directory[3] puts my_thing.field_1 

The advantage of creating such a structure for storing your data is that you can do whatever you want with each element - put them in arrays, hashes, whatever, and still access each individual element and its fields using object.fieldname designation.

+3
source share

All Articles