# Accessing columns by index

**URL:** <https://www.ruby-forum.com/t/accessing-columns-by-index/129516>\
**Category:** Rails\
**Created:** [February 19, 2008, 7:01am UTC](https://www.ruby-forum.com/t/accessing-columns-by-index/129516 "2008-02-19T07:01:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 19, 2008, 7:01am UTC](https://www.ruby-forum.com/t/accessing-columns-by-index/129516/1 "2008-02-19T07:01:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 19, 2008, 9:16am UTC](https://www.ruby-forum.com/t/accessing-columns-by-index/129516/2 "2008-02-19T09:16:48Z")

</div>

On Feb 19, 2008, at 7:01 , Sandeep G. 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

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 19, 2008, 5:09pm UTC](https://www.ruby-forum.com/t/accessing-columns-by-index/129516/3 "2008-02-19T17:09:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 19, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/accessing-columns-by-index/129516/4 "2008-02-19T18:56:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 19, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/accessing-columns-by-index/129516/5 "2008-02-19T19:01:47Z")

</div>

> \<%= 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

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 19, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/accessing-columns-by-index/129516/6 "2008-02-19T20:42:49Z")

</div>

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 G. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 20, 2008, 2:33am UTC](https://www.ruby-forum.com/t/accessing-columns-by-index/129516/7 "2008-02-20T02:33:41Z")

</div>

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 G. \<rails-mailing-l…@andreas-

Thanks Andy for the explanation.

Regards,  
Sandeep G

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 19, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/accessing-columns-by-index/129516/8 "2008-02-19T20:38:36Z")

</div>

Sorry. I misplaced a parenthesis.

On Feb 19, 1:01 pm, Sandeep G. \<rails-mailing-l…@andreas-
