You could check to see if the object responds to "field=".
That's the standard way this works, and is probably sufficient for
most cases. It's important to remember that (in general) it's not
variables/fields in Ruby which have properties like 'read-only' -
that behaviour is defined by methods of the enclosing class, and
methods to read and write variables aren't limited to being named
after those variables. The follow Java-esque method would be
perfectly acceptable code:
class FooBar
# ...
def setMoose(squirrel)
@moose = squirrel
end
end
I'm not aware of any general way to know if a particular method call
will affect a particular variable or not, and I wouldn't be surprised
if the problem turned out to be theoretically equivalent to the
halting problem. At first glance, an equivalence seems trivial; how
to determine if the variable @a is changed by the following method:
def foo
@a = "true" if undecidable_method
end
If you're only worried about the possibility of writing to a
variable, though, then parsetree[1] will give you a lot more coverage
than checking for responding to 'name=', though I expect there are
some pathological cases which might be difficult to catch.
matthew smillie
[1]:
http://rubyforge.org/projects/parsetree/