Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Checking if a field is read-only
Ioana Nap
8/23/2006 9:15:00 AM
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-...
.
2 Answers
Farrel Lifson
8/23/2006 9:23:00 AM
0
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-...
.
>
>
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
Matthew Smillie
8/23/2006 10:33:00 AM
0
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=".
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/...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Checking if a field is read-only
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password