Hi,
It works only with arrays, as I understand.
You can iterate through Arrays or Hashes depends on the situation. In your case you have an ‘Array’ of ‘Hashes’. A very common pattern in ruby. So first you need to iterate through the Array, then you can work with each entry of the array, which is your case is a Hash object. Here is another code sample (simpler tho read IMHO the the other ones):
#!/usr/bin/env ruby
droplets = [{"id"=>1,
"name"=>"MyTestDroplet",
"image_id"=>6918990,
"size_id"=>66,
"region_id"=>9,
"backups_active"=>false,
"ip_address"=>"1.1.1.1",
"private_ip_address"=>nil,
"locked"=>false,
"status"=>"off",
"created_at"=>"2014-12-08T15:40:09Z"},
{"id"=>2,
"name"=>"MyTestDroplet2",
"image_id"=>6918990,
"size_id"=>66,
"region_id"=>7,
"backups_active"=>false,
"ip_address"=>"2.2.2.2",
"private_ip_address"=>nil,
"locked"=>false,
"status"=>"off",
"created_at"=>"2014-12-09T19:27:34Z"}]
# p droplets.class # => Array
# p droplets[0].class # => Hash
counter = 1
# Iterate through Array entries here
droplets.each do |entry|
# Now 'entry' is a hash.
# We access hash values by requesting 'keys'
puts ""
puts "Entry: #{counter}"
puts entry["name"] # => MyTestDroplet ...
puts entry["image_id"] # => 6918990
puts "-"*3
counter += 1
end
Just a note about the use of a separate counter variable.
irb2.1.5> array = %w[ a b c d e ]
#2.1.5 => ["a", "b", "c", "d", "e"]
Array#each gives you one element at a time:
irb2.1.5> array.each {|element| puts "#{element}" }; nil
a
b
c
d
e
If you need the array index of the element, you can use the #with_index method:
irb2.1.5> array.each.with_index {|element, index| puts "#{index}: #{element}" }; nil
0: a
1: b
2: c
3: d
4: e
Which is 0-based by default, but you can also provide an initial value:
irb2.1.5> array.each.with_index(1) {|element, index| puts "#{index}: #{element}" }; nil
1: a
2: b
3: c
4: d
5: e
which is exactly your counter.
droplets.each.with_index(1) do |entry, counter|
# puts various things
end
-Rob
···
On 2014-Dec-9, at 16:01 , Panagiotis Atmatzidis <atma@convalesco.org> wrote:
On 9 Dec 2014, at 22:10, Paul Letskiy <letskiy@gmail.com> wrote:
With array of hashes I get an error:
no implicit conversion of String into Integer (TypeError)
What kind of code gives you this error? Can you show us your snippet?
best regards
Panagiotis (atmosx) Atmatzidis
email: atma@convalesco.org
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5
"As you set out for Ithaca, hope the voyage is a long one, full of adventure, full of discovery [...]" - C. P. Cavafy