Checking if a field is read-only

Hi,
Can you help me please and tell how can i check if a field is read only
in ruby?
i'm trying to write a automated script

Thanks

···

--
Posted via http://www.ruby-forum.com/.

You could check to see if the object responds to "field=".

irb(main):007:0> class Person
irb(main):008:1> attr_reader :name
irb(main):009:1> def initialize(name)
irb(main):010:2> @name = name
irb(main):011:2> end
irb(main):012:1> end
irb(main):014:0> p = Person.new("Farrel")
=> #<Person:0x2db0b48 @name="Farrel">
irb(main):015:0> p.name
=> "Farrel"
irb(main):016:0> p.respond_to?("name=")
=> false

···

On 23/08/06, Ioana Nap <ni@arobs.ro> wrote:

Hi,
Can you help me please and tell how can i check if a field is read only
in ruby?
i'm trying to write a automated script

Thanks

--
Posted via http://www.ruby-forum.com/\.

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/

···

On Aug 23, 2006, at 10:23, Farrel Lifson wrote:

On 23/08/06, Ioana Nap <ni@arobs.ro> wrote:

Hi,
Can you help me please and tell how can i check if a field is read only
in ruby?
i'm trying to write a automated script

You could check to see if the object responds to "field=".