# Design problems (beginner)

**URL:** <https://www.ruby-forum.com/t/design-problems-beginner/53135>\
**Category:** Ruby\
**Created:** [January 26, 2006, 8:38am UTC](https://www.ruby-forum.com/t/design-problems-beginner/53135 "2006-01-26T08:38:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [January 26, 2006, 8:38am UTC](https://www.ruby-forum.com/t/design-problems-beginner/53135/1 "2006-01-26T08:38:07Z")

</div>

I’m working on a medium-sized project and feel like I either:

A. Still don’t understand object-oriented programming

-or-

B. Am duplicating Ruby syntax which I’m not familiar with

First, a little background, (kept mercifully short,) I’m working on a  
survey interviewing program. I’m having having trouble with hammering  
classes into a structure:

Interview  
Questions  
Question  
Answers  
Contents  
RHTML  
Control  
Answers

I’ve been told that ideally, classes like “Answers” shouldn’t know  
anything about other classes like “Control”; but in practice, it’s  
really convenient for Answer’s methods to talk to Control’s public  
methods and vice versa.

Here’s a sexy graph:

Interview.control \<=\> Interview.questions.question.answers

When implementing this, to give these classes a “channel” to talk to  
each other, I’ve ended up passing each “parent” object to the “child”  
object at the child’s creation. Let me show you what I’m talking about:

class Question  
def initialize(interview, questions)  
self.interview = interview  
self.questions = question

```
self.answers = Answers.new(self.interview, self.questions, self)
self.contents = Contents.new(self.interview, self.questions, self)
self.rhtml = RHTML.new(self.interview, self.questions, self)

```

end  
end

In english, to be created, Question demands to be passed its Questions  
“parent” and Interview “grandparent”, then passes these “ancestors” and  
itself on to its children. Thankfully the children don’t have any child  
objects of their own so we’re not creating any four parameter objects,  
but still…

I feel like this must be the programming equivalent of eating at a fancy  
restaurant with your feet. I fear I’m going to be thrown out of the  
programmer’s union.

Please help! Does ruby have a better way to emphasize structure between  
classes?

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [January 26, 2006, 9:12am UTC](https://www.ruby-forum.com/t/design-problems-beginner/53135/2 "2006-01-26T09:12:07Z")

</div>

On 1/26/06, Michael J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> survey interviewing program. I’m having having trouble with hammering  
> classes into a structure:
> 
> Interview  
> Questions  
> Question  
> Answers  
> Contents  
> RHTML  
> Control  
> Answers

Can you give a bit more information as to what your program actually  
does?

I think that you may be over classing this system

I would say you have something like:

Interview: contains collections of questions and answers  
Question: constitutes a single question and associative rules such as  
validation  
Answer: constitutes a single response to a question, and rules such  
as what  
could be considered valid.

Control and Formatting tools seperate…  
Consider a Question#to\_rhtml method or something like that maybe?

This still might be overkill. If your questions always map to the  
questions one to one, you probably don’t need a class for Answers.

I guess what I’d need to know to help is what you’d want these classes  
to do.

There is almost certainly no need for Answers and Questions (the  
collection classes) as an array or hash will most likely hold them  
perfectly well in interview.

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [January 26, 2006, 9:15am UTC](https://www.ruby-forum.com/t/design-problems-beginner/53135/3 "2006-01-26T09:15:08Z")

</div>

On 1/26/06, Gregory B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This still might be overkill. If your questions always map to the  
> questions one to one, you probably don’t need a class for Answers.

s/Answers/Answer

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [January 27, 2006, 1:49am UTC](https://www.ruby-forum.com/t/design-problems-beginner/53135/4 "2006-01-27T01:49:51Z")

</div>

Thanks for your input, Gregory.

I’ve been doing more reading on object-oriented design today and think  
you’re right, I should revamp my project structure into:

Interview  
Question  
Answer

And any cross-talk that currently happens between questions and answers  
needs to be handled in Interview. So I’d have  
Interview.compile\_question, but not Interview.question(name).compile.  
And I’d have Interview.answer[key].validate, but not  
Interview.question(name).validate. As a bonus, it’s less typing too.

* * *

The project is a web-framework for online surveys. Already existing  
open-source projects (like PHPSurveyor) lack basic functionality  
required for survey research. The alternative, commercial packages,  
with basic ones starting at $5,000/year for a lease – make it really  
tough for people with limited means like small business consultants and  
graduate students to do survey research.

So I’m trying to give back to the research community by releasing the  
next version of my company’s in-house interviewing system open-source.  
I’m excited about the project and the work I’ve done on it, but want the  
source code to be reasonably polished too since I expect someday others  
will be looking at it.

Here’s an example of my current test quesitonnaire script:

Q-INTRODUCTION  
Q **Thank you** for your interest in this study. Once you have  
completed the study, please allow up to 7-10 business days for your  
[panel] credit to appear in your [panel] account. Thank you for your  
time and opinions!

Q-SA1  
Q Which of the following best describes your employment status? (MARK  
ONE ONLY.)  
X-1 Employed full-time outside of the home  
X-2 Employed full-time, working from my home  
X-3 Employed part-time outside of the home  
X-4 Employed part-time, working from my home  
X-5 Homemaker  
X-6 Retired  
X-7 Student  
X-8 Not currently employed  
post:  
self.validate :required

skip ‘Q-S1’ if self.questions.current.answers.has\_value? 8

Q-SA  
Q Which, if any, of the following items have you purchased or ordered in  
the past year for work purposes? (MARK ALL THAT APPLY.)  
M-1 Business Cards  
M-2 3-Ring Binders  
M-3 Mailing Labels  
M-4 File Folders  
M-5 None of these  
M-6 Not currently employed  
post:  
self.validate :required  
self.validate :multiple, :params =\> [1…4, 5…6, “none of these/not  
currently employed”]

skip ‘Q-SB’ if self.questions.current.answers.has\_value? 2  
skip ‘Q-S1’

…and so on…

Supports ERB codes too, which is kind of neat.
