In ruby, I have
res = [[0, :product, "client"], [0, :os, "windows"], [0, :architecture, "32rs"]]
I want to get value of os. I can do it by res[1][2], but I don't want to rely on the index as it can change. What I have is the key i.e. :os, so what is the best way to find it?
Assuming you can't change the way the data is structured, you could do something like this:
res.select{|h| h.include?(:os)}.map(&:last)
But it seems to me that your data is better represented with a hash.
res_hash = res.each_with_object({}) do |value, memo|
memo[value[1]] = value[2]
end
# => {:product=>"client", :os=>"windows", :architecture=>"32rs"}
Then you can access discrete data properties like this:
res_hash[:os]
# => "windows"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With