0

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?

5
  • You want to concatenate strings in ruby? If so, you can use "#{get_fru["default_fru_device"]["board_manufacturer"]} #{get_fru["default_fru_device"]["product_name"]}" Commented Jan 27, 2017 at 22:35
  • Thanks - but is there a cleaner way to achieve this? Commented Jan 27, 2017 at 22:36
  • "System" is missing from the string. Is it planned? Commented Jan 27, 2017 at 22:38
  • You can use + (concatenates when used with String): my_string = get_fru["default_fru_device"]["board_manufacturer"]} + " " + get_fru["default_fru_device"]["product_name"]. But @EricDuminil's answer is great. Commented Jan 27, 2017 at 22:38
  • @EricDuminil Sorry, I forgot to include "System". Was a typo. Commented Jan 27, 2017 at 22:49

1 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - nice and clean. Was what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.