Hi, I have my Find statement returning rows like this Col1 Col2 Col3 Col4 While displaying I want to do this order: Cyclic fashion. First row: Col1 Col2 Col3 Col4 Second row: Col2 Col3 Col4 Col1 Third row: Col3 Col4 Col1 Col2 Fourth row: Col4 Col1 Col2 Col3 I thought of doing modulo 4 on column index. But I don''t knw how to access columns by index. I tried this but with no luck: @rows = matrx.find(:all) <% for mat_row in @rows %> <%= h(truncate(mat_row[i modulo 4], 80)) %> <%= h(truncate(mat_row[(i + 1)modulo 4], 55)) %> <%= h(truncate(mat_row[(i+2)modulo 4], 55)) %> <%= h(truncate(mat_row[(i+3)modulo 4], 55)) %> <% end %> Regards, Sandeep -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 19, 2008, at 7:01 , Sandeep Gudibanda wrote:> I thought of doing modulo 4 on column index. But I don''t knw how to > access columns by index. > > I tried this but with no luck: > > @rows = matrx.find(:all) > > <% for mat_row in @rows %><% @rows.each_with_index do |mat_row, i| %> ... <% end %> Also, if you render the collection with render :partial => ''row'', :collection => @rows the index is available in the local variable row_counter. -- fxn --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
In addition to Xavier''s suggestion (each_with_index), you might add an array to help you out with accessing the columns: cols = [:col1, :col2, :col3, :col4] <% @rows.each_with_index do |mat_row, idx| %> <%= h(truncate(mat_row.send cols[i modulo 4], 80)) %> <%= h(truncate(mat_row.send cols[(i + 1)modulo 4], 55)) %> <%= h(truncate(mat_row.send cols[(i+2)modulo 4], 55)) %> <%= h(truncate(mat_row.send cols[(i+3)modulo 4], 55)) %> <% end %> The cols array contains the name of the columns (which become accessors on your ActiveRecord model) in their order for row 1. Here you use it to provide the name of the column in mat_row, and send simply invokes the getter. HTH, AndyV On Feb 19, 3:15 am, Xavier Noria <f...-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> On Feb 19, 2008, at 7:01 , Sandeep Gudibanda wrote: > > > I thought of doing modulo 4 on column index. But I don''t knw how to > > access columns by index. > > > I tried this but with no luck: > > > @rows = matrx.find(:all) > > > <% for mat_row in @rows %> > > <% @rows.each_with_index do |mat_row, i| %> > ... > <% end %> > > Also, if you render the collection with render :partial => > ''row'', :collection => @rows the index is available in the local > variable row_counter. > > -- fxn--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Xavier and Andy. These solutions did not bring any luck though:( <%= h(truncate(mat_row.send cols[i%4], 80)) %> How would this send invoke a getter? Where are we passing symbol of getter? I tried direct copy pasting, got syntax error: wrong number of arguments (1 for 0)) Also each_with_index : I am not sure if i understand this. <% @rows.each_with_index do |mat_row, idx| %> would do give all rows and Indices of all rows? Is that correct? If so, how can we use those indices to access columns in that row? Can you please explain this to me? Regards, Sandeep G -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> <%= h(truncate(mat_row.send cols[i%4], 80)) %> > > How would this send invoke a getter? Where are we passing symbol of > getter? I tried direct copy pasting, got syntax error: wrong number of > arguments (1 for 0))Oh ok. I am sorry my bad. mat_row.send cols[i%4], 80 . IN this 80 is being taken as argument for send. I got this corrected. Thanks a lot for solution. So if no symbol is passed to send, it calls getter? Regards, Sandeep G -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry. I misplaced a parenthesis. On Feb 19, 1:01 pm, Sandeep Gudibanda <rails-mailing-l...@andreas- s.net> wrote:> > <%= h(truncate(mat_row.send cols[i%4], 80)) %> > > > How would this send invoke a getter? Where are we passing symbol of > > getter? I tried direct copy pasting, got syntax error: wrong number of > > arguments (1 for 0)) > > Oh ok. I am sorry my bad. mat_row.send cols[i%4], 80 . IN this 80 is > being taken as argument for send. I got this corrected. > > Thanks a lot for solution. > So if no symbol is passed to send, it calls getter? > > Regards, > Sandeep G > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Actually, you *are* passing a method name (preferably a symbol). The symbol happens to be stored in the cols array. From the inside out, you are: 1. Looking up the i+n modulo 4th item in the cols array 2. Invoking send on the result of the look up (thus getting the value from the nth column as desired). 3. Truncating to the desired length 4. html escaping On Feb 19, 1:01 pm, Sandeep Gudibanda <rails-mailing-l...@andreas- s.net> wrote:> > <%= h(truncate(mat_row.send cols[i%4], 80)) %> > > > How would this send invoke a getter? Where are we passing symbol of > > getter? I tried direct copy pasting, got syntax error: wrong number of > > arguments (1 for 0)) > > Oh ok. I am sorry my bad. mat_row.send cols[i%4], 80 . IN this 80 is > being taken as argument for send. I got this corrected. > > Thanks a lot for solution. > So if no symbol is passed to send, it calls getter? > > Regards, > Sandeep G > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
AndyV wrote:> Actually, you *are* passing a method name (preferably a symbol). The > symbol happens to be stored in the cols array. From the inside out, > you are: > > 1. Looking up the i+n modulo 4th item in the cols array > 2. Invoking send on the result of the look up (thus getting the value > from the nth column as desired). > 3. Truncating to the desired length > 4. html escaping > > On Feb 19, 1:01 pm, Sandeep Gudibanda <rails-mailing-l...@andreas-Thanks Andy for the explanation. Regards, Sandeep G -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---