Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to know if a method is read-write or read-only

Reply
Thread Tools

How to know if a method is read-write or read-only

 
 
Trans
Guest
Posts: n/a
 
      12-18-2005
I've now come across a second usecase for knowing if a method is
read-write or read-only on it's object's state. Both cases boil-down to
having meta-method behave differently depending on the behavior of
method being delegated to, but the circumstances differ completely. To
give an example, the current case generates method wraps for any given
class, applying thread sycronization. If a method is read-only it's
wraped in a Sync::SH, if read-write then Sync::Ex.

So I wonder, is there a way to determine this fact about a method?

Thanks,
T.

 
Reply With Quote
 
 
 
 
Johannes Friestad
Guest
Posts: n/a
 
      12-19-2005
I assume you mean methods that are property getters/setters, along the
lines of 'user.name()'/'user.name=3D(name)', and that read-write or
read-only refer to object properties like 'name'?

There is no general way to determine whether a method updates object
state or not. (Unless you start parsing bytecodes or hook into the VM
parse tree, which is a somewhat larger project.)
You'll have to rely on naming conventions, like "a method that takes
no arguments and has a name not ending with '=3D' is a getter, a method
that takes one argument and has a name ending with '=3D' is a setter".

You can get the number of arguments from Method#arity, like
Array.instance_method(:length).arity
=3D> 0

jf

On 12/18/05, Trans <(E-Mail Removed)> wrote:
> I've now come across a second usecase for knowing if a method is
> read-write or read-only on it's object's state. Both cases boil-down to
> having meta-method behave differently depending on the behavior of
> method being delegated to, but the circumstances differ completely. To
> give an example, the current case generates method wraps for any given
> class, applying thread sycronization. If a method is read-only it's
> wraped in a Sync::SH, if read-write then Sync::Ex.
>
> So I wonder, is there a way to determine this fact about a method?
>
> Thanks,
> T.



 
Reply With Quote
 
 
 
 
Daniel Schierbeck
Guest
Posts: n/a
 
      12-19-2005
Trans wrote:
> I've now come across a second usecase for knowing if a method is
> read-write or read-only on it's object's state. Both cases boil-down to
> having meta-method behave differently depending on the behavior of
> method being delegated to, but the circumstances differ completely. To
> give an example, the current case generates method wraps for any given
> class, applying thread sycronization. If a method is read-only it's
> wraped in a Sync::SH, if read-write then Sync::Ex.
>
> So I wonder, is there a way to determine this fact about a method?
>
> Thanks,
> T.
>

Do you want to know if a method changes its object's state? In that
case, you could duplicate the object, freeze the duplicate and try to
call the method. If an Exception is raised, then either the method has a
bug or it is state-changing.

class Object
def changes_state?(method)
self.dup.freeze.send(method)
return false
rescue TypeError
return true
end
end

"foo".changes_state? :upcase => false
"foo".changes_state? :upcase! => true

It would be nice if a special exception was thrown when attempting to
call a method on a frozen object, so that you could distinguish it from
the usual TypeErrors.

Note that this method isn't very efficient, since it duplicates the
entire object on each invocation. If you were to check many methods on a
single object, you might want to keep the frozen duplicate in an
instance variable.


Cheers,
Daniel
 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      12-21-2005
Thanks Daniel. That's an interesting approach --probably the best
automatic way that can be done.

But I think for a practical approach I will have to create some sort of
annotation declaration and simply manually declare methods as read-only
vs. read-write. Not ideal, of course, but at least it will work
efficiently.

Thanks,
T.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
Help with OutputStream.send() method. I want to know how much data this method already sent. Dalton Barreto Java 4 11-07-2006 12:08 PM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM
Don know Perl, don't know what's broke - re - type1inst!! DP Perl 0 07-17-2003 10:22 PM



Advertisments