From: Paul Roche Date: 2010-10-25T03:10:18+09:00 Subject: Re: Utilizing data from a csv file 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) > 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 Thanks Jermey, this example has helped. This did the trick...... def self.build_all(songs) new_items = [] songs.each { |item| new_items << Song.new(item.name, item.owner) } new_items end -- Posted via http://www.ruby-forum.com/.