Hash to list fails

Why does this output the same hash again (like hash.inspect) and not each key as I want?

@myhash.each { |k,v| "<li>" + k + "</li>" }

Because it is showing the return value of the method each, not what you are doing in the block. You need to build up a string in the block which is the concatenation of your li strings. Crudely, something like str = "" @myhash.each { |k,v| str += "<li>#{k}</li>" }

Colin

Or since only keys are needed, use each_key iterator.

Also, I think ri should say that the “method” each “returns” the same Hash on which you called the method.

-Kedar

Kedar Mhaswade wrote in post #981823:

Or since only keys are needed, use each_key iterator.

Also, I think ri should say that the "method" each "returns" the same Hash on which you called the method.

-Kedar

I actually need both.

Hmm. This should output a string in view but it doesn't:

<% h = { "a" => 100, "b" => 200 } %> <%= h.each {|key, value| puts "#{key} is #{value}" } %>

Kedar Mhaswade wrote in post #981823:

Or since only keys are needed, use each_key iterator.

Also, I think ri should say that the “method” each “returns” the same

Hash

on which you called the method.

-Kedar

I actually need both.

Sure, each or each_pair.

Hmm. This should output a string in view but it doesn’t:

<% h = { “a” => 100, “b” => 200 } %> First: Consider using Symbols (not required, but ubiquitous) as that is the Railism :wink:

<%= h.each {|key, value| puts “#{key} is #{value}” } %> That’s perhaps because you’re dealing with output stream, (and not view of a controller) of your Rails server when you use puts.

You should collate the response in a variable like Colin showed and just do:

<%=str %> which puts the contents of the string in the view.

HTH,

-Kedar

It works as a ruby script. But not in Rails. Strange.

Colin Law wrote in post #981819:

It works as a ruby script. But not in Rails. Strange. Or rather it works in Rails too. But does not do what you want. No wait, but it does do what you told it to do :slight_smile:

-Kedar

It certainly won't do what you think it should....

As has already been pointed out, the .each method on a hash returns the original hash, so your code is going to put some text onto the console (not the same as rendering in the browser) and then render the original hash in the browser.

You *probably* want something more like:   <% test_hash = { "a" => 100, "b" => 200 } %>   <% test_hash.each do |key, value| %>     <%=puts "#{h key} is #{h value}" %>   <% end%>

Or use .inject to collect up all the return values of the block and render that...

Check out the api for lots more handy Hash/Enumerable methods, and *exactly* how they should work. I can heartily recommend playing with them in a console window to get to grips with how they work.

PS "h" is a bad name for a variable, and not idiomatic. It would replace the "h" shortcut for html encoding (assuming Rails 2.x) and isn't very descriptive of it's intention

Of course... without the "puts" :-/

  <%= "#{h key} is #{h value}" %>

Just in case it is not clear from the other replies, when you execute puts when running as a rails app the output will go to the server terminal window, that is the terminal where you typed rails server. If you look there you will probably see it mixed up with all the server stuff.

Colin

can’t you use collect? and then join the result?

@myhash.each {|k,v| “

  • #{k} is #{v}
  • ”}.join

    ooops. sorry. should be

    @myhash.collect {|k,v| “

  • #{k} is #{v}
  • ”}.join