# Checking if a field is read-only

**URL:** <https://www.ruby-forum.com/t/checking-if-a-field-is-read-only/66101>\
**Category:** Ruby\
**Created:** [August 23, 2006, 11:15am UTC](https://www.ruby-forum.com/t/checking-if-a-field-is-read-only/66101 "2006-08-23T11:15:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ioananap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/45deac/32.png) [@ioananap](https://www.ruby-forum.com/u/ioananap)\
**Post date:** [August 23, 2006, 11:15am UTC](https://www.ruby-forum.com/t/checking-if-a-field-is-read-only/66101/1 "2006-08-23T11:15:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ioananap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/45deac/32.png) [@ioananap](https://www.ruby-forum.com/u/ioananap)\
**Post date:** [August 23, 2006, 11:25am UTC](https://www.ruby-forum.com/t/checking-if-a-field-is-read-only/66101/2 "2006-08-23T11:25:00Z")

</div>

On 23/08/06, Ioana N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

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

---

<div class="post-metadata">

**Author:** ![ioananap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/45deac/32.png) [@ioananap](https://www.ruby-forum.com/u/ioananap)\
**Post date:** [August 23, 2006, 12:35pm UTC](https://www.ruby-forum.com/t/checking-if-a-field-is-read-only/66101/3 "2006-08-23T12:35:07Z")

</div>

On Aug 23, 2006, at 10:23, Farrel L. wrote:

> On 23/08/06, Ioana N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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=”.

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](http://rubyforge.org/projects/parsetree/) 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
