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| “

  • ” + k + “
  • ” }

    On 15 February 2011 16:46, Paul B. [email protected] wrote:

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

    @myhash.each { |k,v| “

  • ” + k + “
  • ” }

    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 += “

  • #{k}
  • ” }

    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 M. 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}” } %>

    On Tue, Feb 15, 2011 at 10:26 AM, Paul B.
    [email protected]wrote:

    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.

    On Tue, Feb 15, 2011 at 11:20 AM, Paul B.
    [email protected]wrote:

    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

    On 15 February 2011 18:26, Paul B. [email protected] wrote:

    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}” } %>

    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.

    http://ruby-doc.org/core/classes/Hash.html
    http://ruby-doc.org/core/classes/Enumerable.html

    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

    On 15 February 2011 19:54, Michael P. [email protected] wrote:

    You probably want something more like:
    <%=puts “#{h key} is #{h value}” %>

    Of course… without the “puts” :-/

    <%= “#{h key} is #{h value}” %>

    On 15 February 2011 18:26, Paul B. [email protected] wrote:

    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}” } %>

    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

    On Wed, Feb 16, 2011 at 12:46 AM, Paul B.
    [email protected]wrote:

    “Ruby on Rails: Talk” group.
    To post to this group, send email to [email protected]
    To unsubscribe from this group, send email to
    [email protected]
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.

    ooops. sorry. should be

    @myhash.collect {|k,v| “

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

    On Wed, Feb 16, 2011 at 8:19 AM, Jim Ruther N. [email protected]
    wrote:

    @myhash.each { |k,v| “

  • ” + k + “
  • ” }
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.

    visit my blog at http://jimlabs.heroku.com

    Colin L. wrote in post #981819:

    On 15 February 2011 16:46, Paul B. [email protected] wrote:

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

    @myhash.each { |k,v| “

  • ” + k + “
  • ” }

    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 += “

  • #{k}
  • ” }

    Colin

    Thanks. I didn’t see your respons until now. :slight_smile:

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs