# Accessing one class from another class

**URL:** <https://www.ruby-forum.com/t/accessing-one-class-from-another-class/167166>\
**Category:** Ruby\
**Created:** [May 4, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/accessing-one-class-from-another-class/167166 "2009-05-04T23:39:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [May 4, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/accessing-one-class-from-another-class/167166/1 "2009-05-04T23:39:25Z")

</div>

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:  
  
  
  
Alice  
WonderLand  
  
\<Course difficulty="\>  
Math III  
B+  
  
dt:Tuition  
dt:CurrencyUSD\</dt:Currency\>  
dt:Amount500\</dt:Amount\>  
dt:Period3 months\</dt:Period\>  
\</dt:Tuition\>  
  
  
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  
@status  
@person - access Person  
end

class Person  
end

class Course  
end

class Tution  
end

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 = Record.new

I want to be able to do things like:  
r.student.person.name

???

Thank you

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [May 4, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/accessing-one-class-from-another-class/167166/2 "2009-05-04T23:42:32Z")

</div>

Also to clarify xml tag like can be repeated in my xml file,  
so:

r.student.course  
could return an array of objects of class Course.

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [May 5, 2009, 12:05am UTC](https://www.ruby-forum.com/t/accessing-one-class-from-another-class/167166/3 "2009-05-05T00:05:12Z")

</div>

To clarify Futhre I’m doing somethign like this right now:

class Base

attr\_accessor :name, :value

def initialize  
@name  
@value  
end  
end

class Student  
attr\_accessor :person, :tuition

def initialize  
@person = Person.new  
@tuition = Tuition.new  
end

end

… 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

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [May 5, 2009, 12:08am UTC](https://www.ruby-forum.com/t/accessing-one-class-from-another-class/167166/4 "2009-05-05T00:08:28Z")

</div>

> Also to clarify xml tag like 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):

xml-object:  
[http://github.com/jordi/xml-object/tree/master](http://github.com/jordi/xml-object/tree/master)

happy-mapper:  
[http://happymapper.rubyforge.org/](http://happymapper.rubyforge.org/)

xml-mapping:  
[http://xml-mapping.rubyforge.org/](http://xml-mapping.rubyforge.org/)

Hope it helps  
Cheers

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [May 5, 2009, 8:33am UTC](https://www.ruby-forum.com/t/accessing-one-class-from-another-class/167166/5 "2009-05-05T08:33:53Z")

</div>

On 04.05.2009 23:39, Nick da G wrote:

> 500 class Student class Tution r.student.person.name

You can define attributes via attr\_accessor, e.g.

class Student  
attr\_accessor :person  
end

class Person  
attr\_accessor :name  
end

st = Student.new  
pe = Person.new  
pe.name = “Nick”  
st.person = pe

etc.

As a shortcut you can as well use Struct:

Student = Struct.new :person  
Person = Struct.new :name

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [May 5, 2009, 12:40am UTC](https://www.ruby-forum.com/t/accessing-one-class-from-another-class/167166/6 "2009-05-05T00:40:21Z")

</div>

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  
remember  
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.
