rubyhashruby-2.5

Ruby key getting replaced, instead of a new key created


ruby 2.5

I have the following code:

test = {'primer' => 'grey'}
layers = ["tan","burgundy"]
fillers = ["blue","yellow"]
layers.each do |l|
    fillers.each do |f|
      test[l] = {} if !test.respond_to?(l)
      test[l][f] = {} if !test[l].respond_to?(f)
    end
end

When I run it in irb, I get the following:

{"primer"=>"grey", "tan"=>{"yellow"=>{}}, "burgundy"=>{"yellow"=>{}}}

I am expecting:

{"primer"=>"grey", "tan"=>{"blue"=>{},"yellow"=>{}}, "burgundy"=>{"blue"=>{},"yellow"=>{}}}

Why does the first respond_to produce the key, when the second one, replaces the previous key?

What am I missing?


Solution

  • The expression

    test.respond_to?(l)
    

    does not make sense. l is a string, and respond_to? returns true if the receiver has a method of the name represented by this string. Since the receiver is a Hash and a Hash has no methods Hash#tan and Hash#burgundy, the test will always fail.

    Maybe you want to do a test.has_key?(l) instead....