Hello all,
Pretty much new to Ruby, and I think I am not grasping something
fundamental
here. I’ve written some classes that grab meta tags from various audio
file
formats, and overrode 'to_s" to dump the tags to the console, and “to_a”
to
dump the tags to an array for use elsewhere in my program.
I am not sure how to explain my problem, so I will just paste in a
session
from irb that shows it:
irb> require ‘sneetchalizer’ # my class definitions…
irb> x =OggMetaTags.new(’/home/music/e/elliottSmith-shootingStar.ogg’)
=> #<OggMetaTags:0xa7cc1ac8 […rest omitted…]
irb> x.to_a
=> ["", “”, “”, “”, “”, “”, “”]
irb> x.title
=> “Shooting Star”
irb> x.to_a
=> [“Shooting Star”, “”, “”, “”, “”, “”, “”]
irb> x.artist
=> “Elliott Smith”
irb> x.to_a
=> [“Shooting Star”, “Elliott Smith”, “”, “”, “”, “”, “”]
So you can see I need to explicitly call each attribute before my array
gets
the value. How can I change this so the array is populated automatically
when
called?
Here is the class:
class OggMetaTags < MetaTags
def initialize(filename)
@filename = filename
require ‘ogginfo’
@tags = OggInfo.new(@filename)
end
def title
@title = @tags.tag[‘title’]
end
def artist
@artist = @tags.tag[‘artist’]
end
def album
@album = @tags.tag[‘album’]
end
def genre
@genre = @tags.tag[‘genre’]
end
def year
@year = @tags.tag[‘date’]
end
def comment
@comment = super
end
def tracknum
@tracknum = @tags.tag[‘tracknumber’]
end
def to_a
["#@title", “#@artist”, “#@album”, “#@genre”, “#@year”, “#@comment”,
“#@tracknum”]
end
end
If you need more info or want to see all the code please just ask.
Thanks for
consideration,
-d