1

I have a nested hash like this where it can have 50 values:

{
 "pool1"=>{"name"=>"my-pool", "partition"=>"Common"},
 "pool2"=>{"name"=>"test-2", "partition"=>"baas"}
}

I am trying to get all of the pools with the key "partition"=>"baas". like this:

 {"pool7"=>{"name"=>"test-7", "partition"=>"baas"}
 {"pool12"=>{"name"=>"test-12", "partition"=>"baas"}
 {"pool18"=>{"name"=>"test-18", "partition"=>"baas"}}

This is how i am trying to do this.

def test(partition_name,hash)
        a = 1
        b = partition_name
        c = {}
        d = 1

        partition_hash.each do |i|

           f = i.fetch(":pool#{a}",{}.fetch(:partition, false))

            if f = b
                c["pool#{d + 1}"] = i
                d = d +1
            end
            a = a +1
        end
end

I now get an error like this:

[no implicit conversion of String into Integer]

How can I solve this?

ray
  • 5,454
  • 1
  • 18
  • 40
Daan R.
  • 35
  • 6
  • https://stackoverflow.com/questions/2244915/how-do-i-search-within-an-array-of-hashes-by-hash-values-in-ruby this question can help you – Yurii Verbytskyi Oct 17 '19 at 11:03

1 Answers1

2

You need just select for that:

p hash.select { |_, value| value['partition'] == 'baas' }
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59