Flattening hash into string in Ruby -


Is there a way to level the hash into a string, in which the alternate delimiter between keys and values, and key / value Are you?

For example, print {: a = & gt; : B, C = & gt; : D}. Fletton ('=', '& amp;') should print a = b & c = d

I wrote some code to do this, But I was wondering if there was a clear way:

  class hash df flatten (keywildmeter, entry delimiter) string = "self" Ish, key, value | Key = "# {entrydelimiter} # {key}" If the string! Fleeton ('=', 'and') = => C = d & amp; a = b ' 

thanks

I can not override .flaton , Which is already defined:

Returns a new array which is a one-dimensional flat of this hash, that is, for every key or value, which is an array, Remove in the array. Unlike array # flatten, this method does not flatten by default if it is alternate. That level determines the level of recycling to level the argument.

This is the easiest way of doing this that I know about:

  {: A => 100 ,: b => 200} .map {| k, v | "# {k} = # {v}"}. Join ('and') # => "a = 100 and b = 200 " 

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -