# Read file into array

**URL:** <https://rubytalk.org/t/read-file-into-array/42185>\
**Category:** ruby-talk\
**Created:** [14 November 2007 01:58 UTC](https://rubytalk.org/t/read-file-into-array/42185 "2007-11-14T01:58:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Scott1](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@Brian\_Scott1](https://rubytalk.org/u/Brian_Scott1)\
**Post date:** [14 November 2007 01:58 UTC](https://rubytalk.org/t/read-file-into-array/42185/1 "2007-11-14T01:58:37Z")

</div>

Hello,

What are examples of reading a csv file (Comma) into an Array

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [14 November 2007 04:05 UTC](https://rubytalk.org/t/read-file-into-array/42185/2 "2007-11-14T04:05:01Z")

</div>

On the Internet.

> **···**
>
> On Nov 13, 6:58 pm, Brian Scott \<cui...@network32.net\> wrote:
> 
> > What are examples of reading a csv file (Comma) into an Array

---

<div class="post-metadata">

**Author:** ![Anil\_Bhat](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Anil\_Bhat](https://rubytalk.org/u/Anil_Bhat)\
**Post date:** [14 November 2007 06:05 UTC](https://rubytalk.org/t/read-file-into-array/42185/3 "2007-11-14T06:05:03Z")

</div>

May be the following code will help you:  
def member\_data  
&nbsp;&nbsp;&nbsp;&nbsp;member = Member.find(:all)  
&nbsp;&nbsp;&nbsp;&nbsp;stream\_csv do |csv|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;csv \<\< ["First Name","Last Name","Email"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;member.each do |u|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;csv \<\< [u.first\_name,u.last\_name,u.email]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

Explanation: Member is the model name. First name, last name and email  
are the fields shown while downloading CSV.  
Following code to be written in rhtml file:  
\<%= link\_to 'download', :controller =\> 'reports', :action =\>  
'member\_data' %\>

> **···**
>
> On Nov 14, 9:05 am, Phrogz \<phr...@mac.com\> wrote:
> 
> > On Nov 13, 6:58 pm, Brian Scott \<cui...@network32.net\> wrote:
> > 
> > \> What are examples of reading a csv file (Comma) into an Array
> > 
> > On the Internet.
