S
Shaun Fanning
I'm trying to figure out how to use Ruby to implement a strategy type
pattern that I used in PHP. Basically I took a set of class names passed in
as variables and instantiated the right class depending on the value of the
variable. It was roughly something like:
Class SurveyQuestion
drawQuestion
storeResponse
reportResponse
...
Class SurveyQuestionMultiChoice extends SurveyQuestion
Class SurveyQuestionCheckBox extends SurveyQuestion
...etc.
//build a list of question types based on what the user just submitted
$these_survey_questions = array('MultiChoice', 'MultiChoice', 'CheckBox')
For each $these_survey_questions as $index=$question_type
$class_name = "SurveyQuestion".$question_type
$q = new $class_name()
$q.storeResponse($response_from_this_user)
So I'm struggling to figure out how this type of thing would typically be
done using Ruby. Or maybe it's a bad approach to this type of situation to
begin with so feel free to offer an alternative. Thanks.
Shaun
pattern that I used in PHP. Basically I took a set of class names passed in
as variables and instantiated the right class depending on the value of the
variable. It was roughly something like:
Class SurveyQuestion
drawQuestion
storeResponse
reportResponse
...
Class SurveyQuestionMultiChoice extends SurveyQuestion
Class SurveyQuestionCheckBox extends SurveyQuestion
...etc.
//build a list of question types based on what the user just submitted
$these_survey_questions = array('MultiChoice', 'MultiChoice', 'CheckBox')
For each $these_survey_questions as $index=$question_type
$class_name = "SurveyQuestion".$question_type
$q = new $class_name()
$q.storeResponse($response_from_this_user)
So I'm struggling to figure out how this type of thing would typically be
done using Ruby. Or maybe it's a bad approach to this type of situation to
begin with so feel free to offer an alternative. Thanks.
Shaun