ruby - How to iterate through a Hashie::Mash? -


i'd loop through keys , values of hashie::mash.

here's attempt so:

html = "just @ these subscriptions!" client.subscriptions.each |sub|   html << sub.each |key, value|       html << key.to_s       html << " = "       html << value.to_s   end end 

it returns following error:

"can't convert hashie::mash string (hashie::mash#to_str gives nilclass)"

i've tried sub.keys.to_s , sub.values.to_s produced ["key1", "key2", "key3"], ["value1", "value2", "value3"], i'm looking shows pairs matched, in hash, or ["key1: value1", "key2: value2", "key3: value3"]. there way without zipping separate arrays?

thanks!

sub.to_hash shows exactly hash. :) can whatever can hash; like

html << sub.to_hash.to_s 

or you're doing, in bit more rubyish way:

html << sub.map { |key, value| "#{key} = #{value}" }.join(", ") 

however, real problem in html << sub.each ...: each returns collection being iterated, you're doing html << sub; , fails. code work if removed html << line, since concatenation taken care of inside each loop.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -