I’ve never done any straight ruby coding – only worked with the Rails framework.
I am not sure how to describe the relationships between Classes, other than inheritance relationships.
For example, a School object may have many Student objects.
I would like to be able to make calls like “myschool.student[2].first_name” and “mystudent.school.address”
It may be that I am confusing OOP with elements of relational databasing, so sorry if I’m way off.
I’m not 100% sure what the question is here…
For the first example,
myschool.students[2].first_name, yourSchoolclass needs an accessor for astudentsfield, which needs to be an array (or something else that supports subscripts) e.g.The above allows
myschool.students[2]to return something. Assuming thatstudentscontains instances of aStudentclass, that class might be something like this:Now your example,
myschool.students[2].first_name, should work.For the second example,
mystudent.school.address, you need to have aschoolfield in theStudentclass and anaddressfield in theSchoolclass.The tricky bit is that the
SchoolandStudentinstances point to each other, so you need to set up those references at some point. This would be a simple way:You still need to add the
addressfield and possibly some other stuff that I missed, but that should be easy enough to do.