PREP 2014 Question Authoring - Archived

ans_rule vs. $var->ans_rule

ans_rule vs. $var->ans_rule

by Gavin LaRose -
Number of replies: 0

This has come up a couple of times, so I thought I'd add it to the forum for all to see. The question is: "What is the difference between \{$answers->ans_rule\} and \{ans_rule\}?"

The short answer is "nothing, really." The use of ans_rule, or ans_rule(), is the original way that answer blanks were created in a problem. ans_rule is a function (which takes an argument—the length of the answer blank) to create an answer blank in a problem. The other incantation, $var->ans_rule(), is calling the ans_rule method of a MathObject. This has the same effect: it creates an answer blank (with an optional argument giving the length of the blank). It codes in an explicit correspondence between the MathObject (the correct answer) and the answer blank.

In most cases it doesn't matter which of these you use. There are a couple of instances (e.g., with MultiAnswer objects, which we'll see later in the workshop) in which it's necessary, but in most cases it's done more for aesthetics. In a future version of WeBWorK using $var->ans_rule() may result in the size of the answer blank being determined by the correct answer, or perhaps some other feature that is determined by the answer.

Gavin