As the final result I need a hash like:
{items: {lorem: val1, dolor: val2}, {lorem: val1, dolor: val2}...}
eg a hash of hashes.
Put the problem is it should be created from iteration like this:
@result = []
@goods = {lorem: "ipsum", dolor: "sit"}
@items.each do |item|
@goods.map do |k, v|
if item.title == "ipsum"
@result << [k, v]
else
@result << [k, item.title]
end
end
end
But this is not what I'm searching for, as @result is Array, and it's not 2-dimensional (as result i have [[foo, bar] [foo1,bar1]...])
I see how k
could be converted to Hash key (k.to_sym), but my problem is to have Hash of hashes as the final output.
Ruby 1.9.3 (and Rails, but I believe this could be done without any Rails additions)
Many thanks.
The format of the expected result is not a valid Hash.
Instead of
{items: {lorem: val1, dolor: val2}, {lorem: val1, dolor: val2}...}
It should be like this
{items: [{lorem: val1, dolor: val2}, {lorem: val1, dolor: val2}]}
It's also not hard to get the result you want
@items = []
@goods = {lorem: "ipsum", dolor: "sit"}
@items << @goods
@result = {items: @items}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.