Forum: Ruby accessing one class from another class

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
nick g. (Guest)
on 2009-05-05 01:39
Hello, All.

I'm trying to consuming an xml file and map it to a Ruby object that
would describe that xml file.

What would be the best way to implement the following scenario.

Let's say I have several classes: Student, Course, Person, Tuition
Xml for this would look like this:
<Student status="good">
  <Course difficulty=">
    <CourseName>Math III</CourseName>
    <dt:Period>3 months</dt:Period>
So basically I can parse the xml file using rexml and access all of my
xml tags and stuff.

Here is the Object I want to map it to:

class Student
 @person - access Person

class Person

class Course

class Tution

So the question is how do I access from class Student - class Person? So
that when I down the line later when I parse the file and do:

r =

I want to be able to do things like:


Thank you
nick g. (Guest)
on 2009-05-05 01:42
Also to clarify xml tag like <Course> can be repeated in my xml file,

could return an array of objects of class Course.
nick g. (Guest)
on 2009-05-05 02:05
To clarify Futhre I'm doing somethign like this right now:

class Base

  attr_accessor :name, :value

  def initialize

class Student
  attr_accessor :person, :tuition

  def initialize
    @person =
    @tuition =


... define Tuition & Person class. I'm just looking for a better way to
do it.

Also how would I properly define :course within Student, considering
that I might have a single Course or multiple - depending on the record.

Thank you
Alex Eiras (Guest)
on 2009-05-05 02:08
(Received via mailing list)
> Also to clarify xml tag like <Course> can be repeated in my xml file,
> so:
> r.student.course
> could return an array of objects of class Course.
> --

Hi Nick,

There are some good libraries to accomplish this. You may want to have a
look at one of these (ordered by my own preferences):




Hope it helps
nick g. (Guest)
on 2009-05-05 02:40
Thank you Alex, but no go :-(

I looked into all of them - they all lack in some respect:
happy-mapper - can't build xml & handle namespaces
xml-object can't handle namespaces - and some other issue - can't
xml-mapping - same thing
roxml - same problem.

So the solution is to just do it myself. However that wasn't my question
- it was more a of generic ruby class question.
Robert K. (Guest)
on 2009-05-05 10:33
(Received via mailing list)
On 04.05.2009 23:39, Nick da G wrote:
> <Student status="good">
>     <dt:Amount>500</dt:Amount>
> class Student
> class Tution
You can define attributes via attr_accessor, e.g.

class Student
   attr_accessor :person

class Person
   attr_accessor :name

st =
pe = = "Nick"
st.person = pe


As a shortcut you can as well use Struct:

Student = :person
Person = :name

Kind regards

This topic is locked and can not be replied to.