If object is a string that names a class you will have to use const_get
to avoid eval. Check the archives for a good way to do that using inject.
test_values.each do |value|
instance.send("#{test_attribute}=", value)
instance.send("#{test_attribute}_confirmation=", value)
AFAIK there is no better way to call setter methods, if the name of the
method is a parameter. If you only need to set the instance vars, you
can use #instance_variable_set, but that's not the same thing as calling
the accessor method.
HTH.
···
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
} Is there an neater way to achieve this without using that clunky eval
} and inline substitution?
}
} def test_confirmation(object, test_attribute, test_values = {},
} attributes = {})
} instance = eval("#{object}.new(attributes)")
} test_values.each do |value|
} instance.send("#{test_attribute}=", value)
} instance.send("#{test_attribute}_confirmation=", value)
} assert !instance.save
} assert instance.errors.invalid?(test_attribute)
} end
} end
}
} Just feels a little clunky and wanted some pointers to improve my ruby.
} Any thoughts would be useful.