From: Jeremy Bopp Date: 2010-10-25T02:58:03+09:00 Subject: Re: Utilizing data from a csv file 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)} 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) new_items = [] items.each { |item| new_items << (item * 10) } 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