From: ES Date: 2005-06-07T07:21:52+09:00 Subject: Re: newbie read.scan (?) question Le 6/6/2005, "Bruce D'Arcus" a �crit: >Brian Schr�der wrote: > >> 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 > >OK, am struggling with translating this to my example. Here's what >I've done: > > articles.read.scan(article_page).each do |url| > article_url = "#{base_url}#{url}" > open(article_url) do |article| > article_text = article.read > title = article_text.scan(title_match).to_s > author = article_text.scan(author_match).to_s > puts "loading #{title} ...\n" > a = Article.new(title, author, article_url) > a.add > end add is a class method (see the definition of self.add, which is the same as saying Article.add), so you would want to call it like Article.add a # Need to pass the new article in. >.... and then: > >File.open("articles.yaml", "w") {|f| YAML.dump(p Article.articles, f)} > >But I get a "undefined method `add'" error. I have that part of the >class defined like so: > >class Article > include Journals > > @@articles = [] > > attr_reader :title, :author, :url > > def initialize(title, author, url) > @title = title > @author = author > @url = url > end > > def self.add(article) > @@articles << article > end > > def self.articles > @@article > end > > ... > >> good luck with ruby, > >Thanks! > >Bruce E -- template void quack(duck& d) { d.quack(); }