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>" } -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 February 2011 16:46, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Tue, Feb 15, 2011 at 8:57 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 15 February 2011 16:46, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > 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 > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. > > -KedarI 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}" } %> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Feb 15, 2011 at 10:26 AM, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> 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 ;)> <%= 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> > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
It works as a ruby script. But not in Rails. Strange. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #981819:> On 15 February 2011 16:46, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> 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>" } > > ColinThanks. I didn''t see your respons until now. :-) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Feb 15, 2011 at 11:20 AM, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>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 :-) -Kedar> > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 February 2011 18:26, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 February 2011 19:54, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You *probably* want something more like: > <%=puts "#{h key} is #{h value}" %>Of course... without the "puts" :-/ <%= "#{h key} is #{h value}" %> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 February 2011 18:26, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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}" } %>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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
can''t you use collect? and then join the result? @myhash.each {|k,v| "<li>#{k} is #{v}</li>"}.join On Wed, Feb 16, 2011 at 12:46 AM, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> 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>" } > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ooops. sorry. should be @myhash.collect {|k,v| "<li>#{k} is #{v}</li>"}.join On Wed, Feb 16, 2011 at 8:19 AM, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> can''t you use collect? and then join the result? > > @myhash.each {|k,v| "<li>#{k} is #{v}</li>"}.join > > > On Wed, Feb 16, 2011 at 12:46 AM, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote: > >> 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>" } >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > > -- > ------------------------------------------------------------- > visit my blog at http://jimlabs.heroku.com >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.