Help needed with this code please

P

Paul Roche

Hi. I have a number of classes that use a module as a mixin. Not sure if
I have my terms correct there. Anyway here is one of the classes......
require 'predicate'
class Song
include Pred
@@songs = [ ]
attr_accessor :name, :album, :artist, :time, :year, :id, :in_libs
def initialize(name, album, artist, time, id, in_libs)
@name = name
@album = album
@time = time
@artist = artist
@id = id
@in_libs = in_libs
end

def to_s
puts "<< #{@name} >> by #{@artist} in their album #{@album}.\n"
end

def self.list
@@songs
end

def self.list_add=(val)
@@songs << val
end

end

.....................................

Here is the module....

module Pred
def isa?(target_class)
instance_of?(target_class)
end
end

........................................
Here is another module that I use to fetch data form classes such as
Song........

module Tuneuts
def self.fetch(item, out = [])
all = Song.list + Actor.list + Album.list + Library.list
case
when item.instance_of?(String)
all.each do |obj|

if obj.name.downcase == item.downcase
then out << obj end end
if (out.length > 1)
then MyErr.new("multiple_answer_error", item, "fetch").do_it end
when item.isa?
all.each {|obj|
if obj.eql?(item) then out << obj end}
else MyErr.new("weird_item", item, "fetch").do_it
end
out.first
end
end
............................


The problem is Song.list (in the module Tuneuts) is returning nothing.
Am I missing something here?

I've attached the whole program as a zip file FYI.

Thanks for taking a look

Attachments:
http://www.ruby-forum.com/attachment/5233/problem.zip
 
P

Paul Roche

Thanks for the reply. I think the problem lies in the library class. In
the def self.build_all method to be specific. Here I need to invoke
self.list_add=(val). I've managed it in the other classes but I am stuck
at Library class
 
P

Paul Roche

Hi. I'm bumping this question again with a simpler twist........

I have these methods......

def self.list
@@songs
end

def self.list_add=(val)
@@songs << val
end

in song.rb

-----------------------

I can successfully fetch in the 'songs'through a CSV file.

I am able to populate an array like this.....

songx = ObjectSpace.each_object(Song).to_a


I want to do the same thing using Song.list_add
 
J

Jeremy Bopp

Hi. I'm bumping this question again with a simpler twist........

I have these methods......

def self.list
@@songs
end

def self.list_add=(val)
@@songs << val
end

in song.rb

-----------------------

I can successfully fetch in the 'songs'through a CSV file.

I am able to populate an array like this.....

songx = ObjectSpace.each_object(Song).to_a


I want to do the same thing using Song.list_add

Your current list_add implementation is designed to allow you to add one
Song instance at a time to the list. It's not clear what you really
want to do here. Maybe what you want to do instead is remove your
list_add method entirely and then have your list method simply return
the list generated from ObjectSpace:

def self.list
ObjectSpace.each_object(Song).to_a
end

With that in place, you wouldn't need to have the list_add method
anymore. Of course a much better solution is to have whatever you're
using to load your CSV information call list_add for each Sing instance
it creates.

BTW, I downloaded your code earlier and tried to take a look at it.
Aside from the fact that you really need to pick an indenting convention
and stick to it (it will make your code *much* easier for others to
read), I'm struck my your use of class variables all over the place.
Why are you doing that, or more generally, what are you trying to
accomplish overall?

-Jeremy
 
P

Paul Roche

Jeremy Bopp wrote in post #956417:
Your current list_add implementation is designed to allow you to add one
Song instance at a time to the list. It's not clear what you really
want to do here. Maybe what you want to do instead is remove your
list_add method entirely and then have your list method simply return
the list generated from ObjectSpace:

def self.list
ObjectSpace.each_object(Song).to_a
end

With that in place, you wouldn't need to have the list_add method
anymore. Of course a much better solution is to have whatever you're
using to load your CSV information call list_add for each Sing instance
it creates.

BTW, I downloaded your code earlier and tried to take a look at it.
Aside from the fact that you really need to pick an indenting convention
and stick to it (it will make your code *much* easier for others to
read), I'm struck my your use of class variables all over the place.
Why are you doing that, or more generally, what are you trying to
accomplish overall?

-Jeremy

Thanks for taking a look.


I acknowledge your points, particularly about indentation.
To be honest, this is school work, not an assigment, more take home work
to experiment with, so the class variables are probably there for me to
improve on it. Could you recommend how I change tese to local variables
and make use of them? I'd like to stick with the list_add method.
Overall I am implementing this to learn differnt aspects such as CSV
files and mixin modules. So this could be considered the goal of the
program. I'd like to keep away from objectspace. This is where I'm stuck
at. I'm currently trying to utilize the list add method and .list. Again
thanks for taking a look. I hope this has mad eit a bit clearere. Any
advice would be welcomed
 
J

Jeremy Bopp

Thanks for taking a look.

No problem. ;-)
I acknowledge your points, particularly about indentation.
To be honest, this is school work, not an assigment, more take home work
to experiment with, so the class variables are probably there for me to
improve on it. Could you recommend how I change tese to local variables
and make use of them? I'd like to stick with the list_add method.
Overall I am implementing this to learn differnt aspects such as CSV
files and mixin modules. So this could be considered the goal of the
program. I'd like to keep away from objectspace. This is where I'm stuck
at. I'm currently trying to utilize the list add method and .list. Again
thanks for taking a look. I hope this has mad eit a bit clearere. Any
advice would be welcomed

I suggest that you try breaking down your experiment into more atomic
parts to start. Then assemble them once you understand things better.
You don't want to muddle things by trying too many things all at once.
Build on a solid foundation.

First of all, mixins aren't the solution for everything. From what I've
seen in this project so far, there doesn't appear to be a need for
making your own unless you want to contrive a need. Of course,
contrivance can torture you later. Given that, I would start with
playing with CSV processing and see if you can figure out how to do your
work without mixins at all.

I noticed that Reader#read_in_songs returns an Array of Song instances.
Whatever code calls that method could store that value into whatever
kind of variable makes sense for future reference and processing. There
should be no need to use a class variable for that, really. Another
option might be to create a SongList class, instances of which are tied
to a particular file containing a list of songs. Then you pass around
instances of that class to methods that will process the songs in the list.

As far as mixins go, I wouldn't bother too much with trying to bake your
own at this point. Your Pred module looks like it should work, but all
it's really doing is providing an alias for the instance_of? method.
Unless you add something more meaningful to it, it's probably best to
just remove it from your code for now.

-Jeremy
 
P

Paul Roche

Hi Jeremy. I'm back with a simplified version of the above problem. Here
is the code.....


Basically I now 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, :eek:wner
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)}
 
R

Rajinder Yadav

Hi Jeremy. I'm back with a simplified version of the above problem. Here
is the code.....


Basically I now 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 =3D reader.read_in_songs(song_csv_file_name)

puts "\nBuilding Libraries..."
libs =3D Song.build_all(songs)

----------------------------------------------

here is the song class.........


class Song
attr_accessor :name, :eek:wner
def initialize(name, owner)
=A0@name =3D name
=A0@owner =3D 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 =3D []

your problem is that you create an empty array above, then below you
iterate same empty array and push nothing into it!
songs.each {|song| songs << Song.new(song.name, song.owner)}

this should get you going in the right direction, assuming your cvs
list is comma separated


# create a test cvs list
#
cvslist =3D []
cvslist << "song1,owner1" << "song2,owner2" << "song3,owner3"

# get song and owner strings
#
cvslist.each do |item|
song,owner =3D item.split /\s*,\s*/
puts song
puts owner
end


--=20
Kind Regards,
Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top