# Utilizing data from a csv file

**URL:** https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588
**Category:** ruby-talk
**Created:** [24 October 2010 14:34 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588 "2010-10-24T14:34:26Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)
#### Post date: [24 October 2010 14:34 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/1 "2010-10-24T14:34:26Z")

</div>

Hi I basically want to create a function that takes in data that has  
been taken in from a csv file.

So I have this on my main file.......

songs = reader.read\_in\_songs(song\_csv\_file\_name)

puts "\nBuilding Libraries..."  
libs = Song.build\_all(songs)

> **···**
>
> ----------------------------------------------
> 
> here is the song class.........
> 
> class Song  
> attr\_accessor :name, :owner  
> def initialize(name, owner)  
> @name = name  
> @owner = owner  
> end
> 
> def to\_s  
> puts " #{@name} #{@owner}"  
> end
> 
> def self.build\_all(songs)
> 
> ## I want to be able to 'create song objects'(?) from the data taken in  
> from the csv file here
> 
> end
> 
> end
> 
> -------------------------------------------
> 
> Here is a rough idea I have so far, but it's returning nothing
> 
> songs = []
> 
> songs.each {|song| songs \<\< Song.new([song.name](http://song.name), song.owner)}  
> -------------------------------
> 
> Do I have the right idea?
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Hassan\_Schroeder](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/hassan_schroeder/32/1838_2.png) [@Hassan\_Schroeder](https://rubytalk.org/u/Hassan_Schroeder)
#### Post date: [24 October 2010 15:20 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/2 "2010-10-24T15:20:03Z")

</div>

> Here is a rough idea I have so far, but it's returning nothing

No kidding 🙂

> songs =   
> songs.each {|song| songs \<\< Song.new(song.name, song.owner)}  
> -------------------------------  
> Do I have the right idea?

:~$ irb

> > songs =

=\>

> > songs.each {|song| puts song }

=\>

> >

What exactly would you expect to get out of an empty array?

> **···**
>
> On Sun, Oct 24, 2010 at 7:34 AM, Paul Roche \<prpaulroche@gmail.com\> wrote:
> 
> --  
> Hassan Schroeder ------------------------ hassan.schroeder@gmail.com  
> twitter: @hassan

---

<div class="post-metadata">

### Author: ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)
#### Post date: [24 October 2010 16:50 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/3 "2010-10-24T16:50:46Z")

</div>

Sure, here's the csv file........

"Songname","owner"

> **···**
>
> ##################  
> "Superfast Jellyfish","mick"  
> "On Melancholy Hill","ken"  
> ######################
> 
> I have a reader class that successfully reads the csv file.
> 
> p songs = reader.read\_in\_songs(song\_csv\_file\_name)
> 
> returns this...
> 
> Superfast Jellyfish mick  
> On Melancholy Hill ken  
> [, ,]
> 
> I then want to call.....
> 
> Song.build\_all(songs)
> 
> I want this method to grab the data in 'songs'.
> 
> I want to implement build\_all in Song class to do this
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)
#### Post date: [24 October 2010 17:56 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/4 "2010-10-24T17:56:02Z")

</div>

Incidentally, this does what I want to achieve

def self.build\_all(songs)

songs = ObjectSpace.each\_object(Song).to\_a

end

but I'd rather keep away from ObjectSpace

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)
#### Post date: [24 October 2010 17:58 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/5 "2010-10-24T17:58:03Z")

</div>

This rightly does nothing. The each method operates over the array  
itself by calling the given block with each of the array elements in  
turn. In this case, songs is an empty array because you set it as such  
just before calling songs.each, so there are no elements for the each  
method to process.

You seem to have a small gap in your understanding of how variables are  
assigned values here. When you set songs to , you have lost the value  
passed in as the songs argument of the build\_all method. Do you see  
what I mean?

Try something even simpler:

def make\_array(items)  
&nbsp;&nbsp;new\_items =   
&nbsp;&nbsp;items.each { |item| new\_items \<\< (item \* 10) }  
&nbsp;&nbsp;new\_items  
end

these\_items = [1, 2, 3, 4]  
those\_items = make\_array(these\_items)

After running this, what are the values of these\_items and those\_items?  
How does that change if you replace "new\_items" with "items" everywhere  
in the make\_array function? Why do you think that happens? From where  
is the value of "item" acquired in the make\_array function? What  
happens if you change these\_items as follows:

these\_items = ['1', '2', '3', '4']

-Jeremy

> **···**
>
> On 10/24/2010 09:34 AM, Paul Roche wrote:
> 
> > Hi I basically want to create a function that takes in data that has  
> > been taken in from a csv file.
> > 
> > So I have this on my main file.......
> > 
> > songs = reader.read\_in\_songs(song\_csv\_file\_name)
> > 
> > puts "\nBuilding Libraries..."  
> > libs = Song.build\_all(songs)
> > 
> > ----------------------------------------------
> > 
> > here is the song class.........
> > 
> > class Song  
> > attr\_accessor :name, :owner  
> > def initialize(name, owner)  
> > @name = name  
> > @owner = owner  
> > end
> > 
> > def to\_s  
> > puts " #{@name} #{@owner}"  
> > end
> > 
> > def self.build\_all(songs)
> > 
> > ## I want to be able to 'create song objects'(?) from the data taken in  
> > from the csv file here
> > 
> > end
> > 
> > end
> > 
> > -------------------------------------------
> > 
> > Here is a rough idea I have so far, but it's returning nothing
> > 
> > songs =
> > 
> > songs.each {|song| songs \<\< Song.new(song.name, song.owner)}

---

<div class="post-metadata">

### Author: ![Greg\_Barozzi](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@Greg\_Barozzi](https://rubytalk.org/u/Greg_Barozzi)
#### Post date: [24 October 2010 18:04 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/6 "2010-10-24T18:04:54Z")

</div>

Paul Roche wrote in post #956704:

> Hi I basically want to create a function that takes in data that has  
> been taken in from a csv file.

Hi Paul,

It looks like you have a handle on your data structure but need help  
parsing the data from your csv file. I wrote a short program do handle  
data from csv files a little while back. Maybe looking it over will help  
you with your own project.

Code:

filename = 'data.csv'  
grades =   
heading =   
values =   
i = 0

file = File.new(filename, 'r')

file.each\_line("\n") do |row|  
&nbsp;&nbsp;columns = row.split(",")  
&nbsp;&nbsp;#p columns  
&nbsp;&nbsp;grades \<\< columns  
end

grades.each do |line|  
&nbsp;&nbsp;heading = line if i == 0  
&nbsp;&nbsp;values = line if i == 1  
&nbsp;&nbsp;if i \> 2  
&nbsp;&nbsp;&nbsp;&nbsp;# Print Unit title and student name  
&nbsp;&nbsp;&nbsp;&nbsp;print values[0], "\nName: ", line[0],",", line[1],"\n\n"

&nbsp;&nbsp;&nbsp;&nbsp;# print total score and final grade  
&nbsp;&nbsp;&nbsp;&nbsp;print "Total Score: ", line[2], " -- ", line[3], "\n\n"

&nbsp;&nbsp;&nbsp;&nbsp;# print breakdown HOURS  
&nbsp;&nbsp;&nbsp;&nbsp;print heading[4], ": ", line[5], " of ", values[4], "\n"

&nbsp;&nbsp;&nbsp;&nbsp;# print breakdown PARTICIPATION  
&nbsp;&nbsp;&nbsp;&nbsp;print heading[5], ": ",line[6], "% of ", values[5], "\n"

&nbsp;&nbsp;&nbsp;&nbsp;# print breakdown Assignment Points  
&nbsp;&nbsp;&nbsp;&nbsp;print heading[6], ": ",line[7], " of ", values[6], " total  
points\n"

&nbsp;&nbsp;&nbsp;&nbsp;# print breakdown Assignments  
&nbsp;&nbsp;&nbsp;&nbsp;print heading[7], ": ",line[8], "% of ", values[7], "\n"

&nbsp;&nbsp;&nbsp;&nbsp;# print breakdown WA Law  
&nbsp;&nbsp;&nbsp;&nbsp;print heading[8], ": ",line[9], "% of ", values[8],  
"\n"

&nbsp;&nbsp;&nbsp;&nbsp;# print breakdown Quiz  
&nbsp;&nbsp;&nbsp;&nbsp;print heading[9], ": ",line[10], "% of ",  
values[9], "\n"

&nbsp;&nbsp;&nbsp;&nbsp;# print breakdown Exam  
&nbsp;&nbsp;&nbsp;&nbsp;print heading[10], ": ",line[11], "% of ",  
values[10], "\n"

&nbsp;&nbsp;&nbsp;&nbsp;# print breakdown EC  
&nbsp;&nbsp;&nbsp;&nbsp;print heading[11].chomp, ": ",line[12], "\n\n\n\n"

&nbsp;&nbsp;&nbsp;&nbsp;#print line  
&nbsp;&nbsp;end  
&nbsp;&nbsp;i += 1  
end

END CODE

It's a little ugly with the formating. I wasn't planning on showing this  
to anyone when I wrote it. It reads the csv file line by line and then  
based on the cell position it stores the data in an array. You should be  
able to substitute my arrays with your class objects no problem.

Hope this helps you.

Greg-

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)
#### Post date: [24 October 2010 20:18 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/7 "2010-10-24T20:18:12Z")

</div>

Having build\_all in Song class was me just trying to see if I could read  
data with the build\_all method.

You're right the old implementation you referred to is what I will be  
working with. This features as you mentioned linking the two lists. I'm  
just going to try and get myself out of this mudddle.

So to be specific.... Here's the original build\_all. This is in the  
library class (there wil be no build\_all method in Song)

(ignore the !)

def self.build\_all  
lib\_objs = ObjectSpace.each\_object(Library).to\_a  
! ! songs = ObjectSpace.each\_object(Song).to\_a  
! ! lib\_objs.each do |library|  
! ! ! songs.each do |song|  
! ! ! ! if song.in\_libs.include?(library)  
! ! ! ! ! then library.songs \<\< song end  
! ! ! end  
! ! ! end  
lib\_objs  
end

Here is part of the new build\_all method....

def self.build\_all(lib, songs)

new\_items\_lib = []  
&nbsp;&nbsp;lib.each { |iteml| new\_items\_lib \<\< Library.new([iteml.name](http://iteml.name),  
iteml.songx) }

new\_items\_song = []  
&nbsp;&nbsp;songs.each { |item| new\_items\_song \<\< Song.new([item.name](http://item.name), item.album,  
item.artist, item.time, item.year, [item.id](http://item.id), item.in\_libs) }

It does what I want it to do for 'songs', but throws up an error for  
'lib'. I'm going to take a few steps back on this

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)
#### Post date: [24 October 2010 15:27 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/8 "2010-10-24T15:27:08Z")

</div>

Hassan Schroeder wrote in post #956709:

> **···**
>
> > On Sun, Oct 24, 2010 at 7:34 AM, Paul Roche \<prpaulroche@gmail.com\> \> wrote:
> > 
> > > Here is a rough idea I have so far, but it's returning nothing
> > 
> > No kidding 🙂
> > 
> > > songs =   
> > > songs.each {|song| songs \<\< Song.new(song.name, song.owner)}  
> > > -------------------------------  
> > > Do I have the right idea?
> > 
> > :~$ irb
> > 
> > > > songs =
> > 
> > =\>
> > 
> > > > songs.each {|song| puts song }
> > 
> > =\>
> > 
> > > >
> > 
> > What exactly would you expect to get out of an empty array?
> 
> Sure, scrap that 🙂  
> If I put Song.new(songs.name, songs.id) into this function I get  
> something. Any idea how I can 'create objects'(?) from the data from the  
> csv file?
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Hassan\_Schroeder](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/hassan_schroeder/32/1838_2.png) [@Hassan\_Schroeder](https://rubytalk.org/u/Hassan_Schroeder)
#### Post date: [24 October 2010 17:38 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/9 "2010-10-24T17:38:27Z")

</div>

?! So, I'd say you need to work on that -- there's no obvious way to  
split either of those into 2 fields.

I would think you'd want to return something per line like e.g.

&nbsp;&nbsp;{:owner=\>"mick", :songname=\>"Superfast Jellyfish"}

which you could then pass to Song.new

> **···**
>
> On Sun, Oct 24, 2010 at 9:50 AM, Paul Roche \<prpaulroche@gmail.com\> wrote:
> 
> > "Songname","owner"  
> > ##################  
> > "Superfast Jellyfish","mick"  
> > "On Melancholy Hill","ken"  
> > ######################
> > 
> > I have a reader class that successfully reads the csv file.
> > 
> > p songs = reader.read\_in\_songs(song\_csv\_file\_name)
> > 
> > returns this...
> > 
> > Superfast Jellyfish mick  
> > On Melancholy Hill ken
> 
> --  
> Hassan Schroeder ------------------------ hassan.schroeder@gmail.com  
> twitter: @hassan

---

<div class="post-metadata">

### Author: ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)
#### Post date: [24 October 2010 17:59 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/10 "2010-10-24T17:59:45Z")

</div>

Aside from the mess of using ObjectSpace like this, for what reason do  
you pass in a value for songs in the first place? 😉

-Jeremy

> **···**
>
> On 10/24/2010 12:56 PM, Paul Roche wrote:
> 
> > Incidentally, this does what I want to achieve
> > 
> > def self.build\_all(songs)
> > 
> > songs = ObjectSpace.each\_object(Song).to\_a
> > 
> > end

---

<div class="post-metadata">

### Author: ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)
#### Post date: [24 October 2010 18:10 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/11 "2010-10-24T18:10:18Z")

</div>

Jeremy Bopp wrote in post #956736:

> **···**
>
> > On 10/24/2010 09:34 AM, Paul Roche wrote:
> > 
> > > ----------------------------------------------
> > > 
> > > end
> > > 
> > > end
> > > 
> > > -------------------------------------------
> > > 
> > > Here is a rough idea I have so far, but it's returning nothing
> > > 
> > > songs =
> > > 
> > > songs.each {|song| songs \<\< Song.new(song.name, song.owner)}
> > 
> > This rightly does nothing. The each method operates over the array  
> > itself by calling the given block with each of the array elements in  
> > turn. In this case, songs is an empty array because you set it as such  
> > just before calling songs.each, so there are no elements for the each  
> > method to process.
> > 
> > You seem to have a small gap in your understanding of how variables are  
> > assigned values here. When you set songs to , you have lost the value  
> > passed in as the songs argument of the build\_all method. Do you see  
> > what I mean?
> > 
> > Try something even simpler:
> > 
> > def make\_array(items)  
> > &nbsp;&nbsp;new\_items =   
> > &nbsp;&nbsp;items.each { |item| new\_items \<\< (item \* 10) }  
> > &nbsp;&nbsp;new\_items  
> > end
> > 
> > these\_items = [1, 2, 3, 4]  
> > those\_items = make\_array(these\_items)
> > 
> > After running this, what are the values of these\_items and those\_items?  
> > How does that change if you replace "new\_items" with "items" everywhere  
> > in the make\_array function? Why do you think that happens? From where  
> > is the value of "item" acquired in the make\_array function? What  
> > happens if you change these\_items as follows:
> > 
> > these\_items = ['1', '2', '3', '4']
> > 
> > -Jeremy
> 
> Thanks Jermey, this example has helped. This did the trick......
> 
> def self.build\_all(songs)
> 
> new\_items =   
> &nbsp;&nbsp;songs.each { |item| new\_items \<\< Song.new(item.name, item.owner) }  
> &nbsp;&nbsp;&nbsp;new\_items
> 
> end
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)
#### Post date: [24 October 2010 18:13 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/12 "2010-10-24T18:13:08Z")

</div>

Greg Barozzi wrote in post #956738:

> Paul Roche wrote in post #956704:
> 
> > Hi I basically want to create a function that takes in data that has  
> > been taken in from a csv file.
> 
> Hi Paul,
> 
> It looks like you have a handle on your data structure but need help  
> parsing the data from your csv file. I wrote a short program do handle  
> data from csv files a little while back. Maybe looking it over will help  
> you with your own project.

Thanks Greg, much appreciated

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)
#### Post date: [25 October 2010 14:24 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/13 "2010-10-25T14:24:32Z")

</div>

> Having build\_all in Song class was me just trying to see if I could read  
> data with the build\_all method.
> 
> You're right the old implementation you referred to is what I will be  
> working with. This features as you mentioned linking the two lists. I'm  
> just going to try and get myself out of this mudddle.

FYI, what you're trying to do with CSV files is often handled with a  
relational database. Have you considered trying out sqlite? There are  
some gems such as ActiveRecord that can make using that database backend  
and others relatively easy.

> Here is part of the new build\_all method....
> 
> def self.build\_all(lib, songs)
> 
> new\_items\_lib =   
> &nbsp;&nbsp;lib.each { |iteml| new\_items\_lib \<\< Library.new(iteml.name,  
> iteml.songx) }
> 
> new\_items\_song =   
> &nbsp;&nbsp;songs.each { |item| new\_items\_song \<\< Song.new(item.name, item.album,  
> item.artist, item.time, item.year, item.id, item.in\_libs) }
> 
> It does what I want it to do for 'songs', but throws up an error for  
> 'lib'. I'm going to take a few steps back on this

If you want some help with that, we'll need to know the details of the  
error. If you're just stating what your working on next though, good  
luck. 🙂

-Jeremy

> **···**
>
> On 10/24/2010 3:18 PM, Paul Roche wrote:

---

<div class="post-metadata">

### Author: ![Leslie\_Viljoen1](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Leslie\_Viljoen1](https://rubytalk.org/u/Leslie_Viljoen1)
#### Post date: [25 October 2010 14:50 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/14 "2010-10-25T14:50:25Z")

</div>

Split is simple, but it doesn't deal with CSV rows which have strings  
with commas in them. Like this:  
apple, pear, "banana, salad", apricot

The FasterCSV gem is an excellent way to read and write CSV files and  
it deals with these sorts of problems: [http://fastercsv.rubyforge.org](http://fastercsv.rubyforge.org)

> **···**
>
> On Sun, Oct 24, 2010 at 8:04 PM, Greg Barozzi \<greg5000@earthlink.net\> wrote:
> 
> > Paul Roche wrote in post #956704:
> > 
> > > Hi I basically want to create a function that takes in data that has  
> > > been taken in from a csv file.
> > 
> > Hi Paul,
> > 
> > It looks like you have a handle on your data structure but need help  
> > parsing the data from your csv file. I wrote a short program do handle  
> > data from csv files a little while back. Maybe looking it over will help  
> > you with your own project.
> > 
> > Code:
> > 
> > filename = 'data.csv'  
> > grades =   
> > heading =   
> > values =   
> > i = 0
> > 
> > file = File.new(filename, 'r')
> > 
> > file.each\_line("\n") do |row|  
> > columns = row.split(",")

---

<div class="post-metadata">

### Author: ![Hassan\_Schroeder](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/hassan_schroeder/32/1838_2.png) [@Hassan\_Schroeder](https://rubytalk.org/u/Hassan_Schroeder)
#### Post date: [24 October 2010 16:06 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/15 "2010-10-24T16:06:50Z")

</div>

I'm not sure I understand the question; Song.new creates an object.

Are you having trouble with the actual values? What does this function  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.read\_in\_songs(song\_csv\_file\_name)

actually return? Can you show an example?

> **···**
>
> On Sun, Oct 24, 2010 at 8:27 AM, Paul Roche \<prpaulroche@gmail.com\> wrote:
> 
> > If I put Song.new(songs.name, songs.id) into this function I get  
> > something. Any idea how I can 'create objects'(?) from the data from the  
> > csv file?
> 
> --  
> Hassan Schroeder ------------------------ hassan.schroeder@gmail.com  
> twitter: @hassan

---

<div class="post-metadata">

### Author: ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)
#### Post date: [24 October 2010 18:10 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/16 "2010-10-24T18:10:18Z")

</div>

Jeremy Bopp wrote in post #956737:

> **···**
>
> > On 10/24/2010 12:56 PM, Paul Roche wrote:
> > 
> > > Incidentally, this does what I want to achieve
> > > 
> > > def self.build\_all(songs)
> > > 
> > > songs = ObjectSpace.each\_object(Song).to\_a
> > > 
> > > end
> > 
> > Aside from the mess of using ObjectSpace like this, for what reason do  
> > you pass in a value for songs in the first place? 😉
> > 
> > -Jeremy
> 
> Yeah good point.
> 
> I don't have my thinking cap on with this ruby stuff 🙂
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)
#### Post date: [24 October 2010 18:16 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/17 "2010-10-24T18:16:36Z")

</div>

You're farther along, but you're still missing something important that  
would allow you to reduce this further. What are the kinds of items in  
the songs list you pass into the build\_all method? Are they arrays of  
strings, numbers, or something else?

-Jeremy

> **···**
>
> On 10/24/2010 01:10 PM, Paul Roche wrote:
> 
> > Jeremy Bopp wrote in post #956736:
> > 
> > > Try something even simpler:
> > > 
> > > def make\_array(items)  
> > > &nbsp;&nbsp;new\_items =   
> > > &nbsp;&nbsp;items.each { |item| new\_items \<\< (item \* 10) }  
> > > &nbsp;&nbsp;new\_items  
> > > end
> > > 
> > > these\_items = [1, 2, 3, 4]  
> > > those\_items = make\_array(these\_items)
> > > 
> > > After running this, what are the values of these\_items and those\_items?  
> > > How does that change if you replace "new\_items" with "items" everywhere  
> > > in the make\_array function? Why do you think that happens? From where  
> > > is the value of "item" acquired in the make\_array function? What  
> > > happens if you change these\_items as follows:
> > > 
> > > these\_items = ['1', '2', '3', '4']
> > 
> > Thanks Jermey, this example has helped. This did the trick......
> > 
> > def self.build\_all(songs)
> > 
> > new\_items =   
> > &nbsp;&nbsp;songs.each { |item| new\_items \<\< Song.new(item.name, item.owner) }  
> > &nbsp;&nbsp;&nbsp;new\_items
> > 
> > end

---

<div class="post-metadata">

### Author: ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)
#### Post date: [25 October 2010 16:15 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/18 "2010-10-25T16:15:40Z")

</div>

It's also been moved into Ruby's core API with 1.9 (require "csv").  
Docs are here: [http://ruby-doc.org/ruby-1.9/classes/CSV.html](http://ruby-doc.org/ruby-1.9/classes/CSV.html)

P.S.: This makes a good case why frames are Evil, too. 😉

> **···**
>
> On Mon, Oct 25, 2010 at 4:50 PM, Leslie Viljoen \<leslieviljoen@gmail.com\> wrote:
> 
> > The FasterCSV gem is an excellent way to read and write CSV files and  
> > it deals with these sorts of problems: [http://fastercsv.rubyforge.org](http://fastercsv.rubyforge.org)
> 
> --  
> Phillip Gawlowski
> 
> Though the folk I have met,  
> (Ah, how soon!) they forget  
> When I've moved on to some other place,  
> There may be one or two,  
> When I've played and passed through,  
> Who'll remember my song or my face.

---

<div class="post-metadata">

### Author: ![Paul\_Roche](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Paul\_Roche](https://rubytalk.org/u/Paul_Roche)
#### Post date: [24 October 2010 19:14 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/19 "2010-10-24T19:14:47Z")

</div>

> You're farther along, but you're still missing something important that  
> would allow you to reduce this further. What are the kinds of items in  
> the songs list you pass into the build\_all method? Are they arrays of  
> strings, numbers, or something else?
> 
> -Jeremy

The larger implementation of this will have strings and numbers

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)
#### Post date: [24 October 2010 19:33 UTC](https://rubytalk.org/t/utilizing-data-from-a-csv-file/60588/20 "2010-10-24T19:33:28Z")

</div>

Am I correct that the answer to my question is that you are passing an  
array of Song instances to the build\_all method? If so, what are you  
planning to do that would change that?

I remember from your earlier code example from the earlier thread that  
you seem to be trying to define per-user libraries of some kind where  
you have one CSV file with a list of songs and another CSV file with a  
list of people. IIRC, your implementation there was trying to link the  
two lists via an ID column shared between the two CSV files.

If this is your goal, I don't understand how this will change your  
method of loading the songs CSV file. Yeah, you'll add some fields to  
your Song implementation, but that shouldn't require you to move the  
instantiation code into a class method of Song any more than your  
current implementation does.

-Jeremy

> **···**
>
> On 10/24/2010 02:14 PM, Paul Roche wrote:
> 
> > > You're farther along, but you're still missing something important that  
> > > would allow you to reduce this further. What are the kinds of items in  
> > > the songs list you pass into the build\_all method? Are they arrays of  
> > > strings, numbers, or something else?
> > > 
> > > -Jeremy
> > 
> > The larger implementation of this will have strings and numbers
