From: "Brian Schröder" Date: 2005-06-07T06:00:47+09:00 Subject: Re: newbie read.scan (?) question On 06/06/05, Bruce D'Arcus wrote: > > > Ghislain Mary wrote: > > > Because your author and title probably aren't strings as you expect them > > to be but rather arrays. > > Ah, right. Using scan returns an array. On this ... > > > > I have these two classes: > > > > > > class Article > > > > > > include Journals > > > > > > attr_reader :title, :author, :description, :url > > > def initialize(title, author, url) > > > @title = title > > > @author = author > > > @url = url > > > end > > > > > > def to_s > > > "#@title, #@author" > > > end > > > > > > def abstract > > > # > > > end > > > > > > def refer > > > Journals::const_get(:BASE_URL) + "/" + > > > @url + "&form=refer&file=file.txt" > > > end > > > > > > def pdf > > > Journals::const_get(:BASE_URL) + "/" + > > > @url + "&form=pdf&file=file.pdf" > > > end > > > end > > > > > > class Articles > > > # > > > attr_reader :articles > > > > > > def initialize > > > @articles = Array.new > > > end > > > > > > def append(article) > > > @articles.push(article) > > > self > > > end > > > > > > def [](index) > > > @articles[index] > > > end > > > end > > > > Why create an Article class and an Articles class? > > Because I'm *real* newbie! My only programming background is with > XSLT. So I'm trying to also understand basic OO design in this > example. > > > You could make all > > the content of your Articles class also content of the Article class but > > at the class level instead of the instance level. So you just have to > > transform your @articles variable into @@articles and define your append > > and [] methods as self.append and self.[]. > > Can you give me an abbreviated example of how to do actually do this? > For example, how do I define @@articles under the Article class, and > how would I then define the append method there. > I have not followed this thread in depth, but I think it is a good idea to distinguish between a set of articles and an article. I don't see how you would benefit from mixing these two. If I understand the proposal correctly, you would no longer be able to maintain two independent sets of articles, because the ArticleSet would be part of the article class. Anyhow, here is how to define a class variable and class methods. class Klass @@foo = [] def self.add(bar) @@foo << bar end def self.foo @@foo end end Klass.add(1) Klass.add(2) p Klass.foo good luck with ruby, Brian -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/