Thursday 15 January 2015

ruby - Turning a hash into a string of name-value pairs -


If I change Ruby hash into a string of name-value pairs (for example, used in HTTP parameters) Am), is this the best way?

Define the hash field = {"a" = & gt; "Foo", "B" = & gt; "Bar"} # Move it to the name-value string http_params = fields.map {| K, v | "# {K} = # {v}"}. ('& Amp;')

I think my question is:

Is there an easy way to go to http_params ? Given, works in the above way and is quite straightforward, but I'm curious if there is any way of getting the string in the string without creating an array (the result of the first map method)?

This is probably the best that you can do. You can repeat through the couple in Josh, you can go as you build the string. But in this case the intermediate string must be created and destroyed in each step.

Do you have any use-cases where this is a performance barrier? In general, Ruby is working behind scenes so that worrying about such a temporary array is probably not worth it. If you are worried that this may be a problem, then your code for speed and memory usage Consider entering, often results do not expect you.


No comments:

Post a Comment