How do i unsubscribe to this

-----Original Message-----
From: Pierre-Charles David [mailto:Pierre-Charles.David / emn.fr]
Sent: 23 July 2001 15:19
To: ruby-talk / ruby-lang.org
Subject: [ruby-talk:18345] Re: Newbie question: Data structures in Ruby.


Erik wrote:
> Hi all,
> 
> I just started implementing some tools in Ruby, however I am having
> difficulties of how to handle complex data structures in Ruby. For
> example, I want to implement the following structure:
> 
> class Page
>     def initialize
>         @articles = []
>     end
>     ...
> end
> 
> class Article
>     def initialize
>         @elements = []
>     end
>     ...
> end
> 
> class Element
>     ...
> end
> 
> In the end, the page should contain a list of articles, and each article
> a list of elements.
> I want to have acces to it like:
> 
> puts currentpage.articles[2].elements[5].content
> 
> I want to be able to manipulate this data structure like f.i.:
> 
> currentpage.articles[2].elements[5].content = 'Hello world'
> 
> I am not sure if or how I can realise this, could somebody please give
> me a hint?


Ruby makes attributes hidden ('private') by default. You need to 
explicitely define reader methods for those you want to make accessible:

class Page
   def articles
     @articles
   end
end

Actually this is so common that Ruby comes with the 'attr_reader' method 
to do this:

class Page
    attr_reader :articles
end

See also methods attr and attr_writer (defined in class Module).

-- 
Pierre-Charles David (pcdavid <at> emn <dot> fr)
DEA Informatique, ±Äole des Mines de Nantes, France