On Jun 11, 2006, at 4:03 PM, James McCarthy wrote: > 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. > You can pass classes as arguments to functions: test_confirmation(SomeClass, ...) instance = object.new(attributes) If the problem is you have to get a string, use const_get e.g. Object.const_get(object).new(attributes) The only problem with this is that it doesn't work for nested constants (e.g. Net::HTTP). So to generalize that you do instance = object.split(/::/).inject(Object) { |base, child| base.const_get(child) }.new(attributes) the #send s seem fine to me, much better than just straight eval'ing the stuff. > -- > Posted via http://www.ruby-forum.com/. >