How to iterate over Hashie :: Mash?
I would like to skip the keys and values of Hashie :: Mash.
Here's my attempt at doing it:
html = "Just look at these subscriptions!"
client.subscriptions.each do |sub|
html << sub.each do |key, value|
html << key.to_s
html << " = "
html << value.to_s
end
end
It returns the following error:
"Cannot convert Hashie :: Mash to String (Hashie :: Mash # to_str gives NilClass)"
I have tried sub.keys.to_s
and sub.values.to_s
who created ["key1", "key2", "key3"]
, ["value1", "value2", "value3"]
but I am looking for something that shows a couple who are the same, because they are in a hash, or the like [ "key1: value1", " key2: value2", "key3: value3"]. Is there a way to do this without compressing the individual arrays?
Thank!
source to share
sub.to_hash
shows something similar to a hash. :) Then you can do whatever you can with the hash; as
html << sub.to_hash.to_s
or you could do what you are doing a little more Rubyish:
html << sub.map { |key, value| "#{key} = #{value}" }.join(", ")
However, your real problem is in html << sub.each ...
: each
returns a duplicate collection, so you do html << sub
; and it fails. Your code will work if you just remove html <<
from that line, since the concatenation is done inside the loop each
.
source to share