Wes
Wes

Reputation: 764

Getting Specific Value From Nested Hash in Ruby

I have a hash that looks like this:

get_fru = 
  {"default_fru_device"=>
    {:name=>"default_fru_device",
    "chassis_type"=>"Other",
    "board_manufacturer"=>"IBM",
    "product_name"=>"System x3650 M4"
    }
   }

I know that if I wanted to get the value of product_name, I could simply do get_fru["default_fru_device"]["product_name"] which would, in this example, return System x3650 M4.

However, If I wanted to get the values IBM and System x3650 M4 and make them display as a single string that said IBM System x3650 M4, how would I go about achieving that?

Upvotes: 0

Views: 386

Answers (1)

Eric Duminil
Eric Duminil

Reputation: 54213

You could use Hash#values_at and Array#join :

get_fru["default_fru_device"].values_at('board_manufacturer', 'product_name').join(' ')
#=> "IBM System x3650 M4"

Upvotes: 2

Related Questions