[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calling an arbitrary method as and when an array's contents change

Dan Stevens (IAmAI)

7/5/2007 10:14:00 PM

I have a collection class that extends the Array class and I wish to
have a particular method to be called whenever the contents of an
instance of the class change. Can anyone advise on how I can achieve
this?

I'm aware that I can compare #hash with a previous saved value to
determine if the contents of the instance have changed since the last
time #hash was called. Therefore, if I can find a means of calling an
arbitrary method or piece of code every time any method is called on
the instance, I could achieve what I'm after. Can I override
Object#send?

17 Answers

Gregory Brown

7/5/2007 10:20:00 PM

0

On 7/5/07, Dan Stevens (IAmAI) <dan.stevens.iamai@gmail.com> wrote:
> I have a collection class that extends the Array class and I wish to
> have a particular method to be called whenever the contents of an
> instance of the class change. Can anyone advise on how I can achieve
> this?
>
> I'm aware that I can compare #hash with a previous saved value to
> determine if the contents of the instance have changed since the last
> time #hash was called. Therefore, if I can find a means of calling an
> arbitrary method or piece of code every time any method is called on
> the instance, I could achieve what I'm after. Can I override
> Object#send?

It sounds like you might be able to set up something like this with Observer.
http://www.ruby-doc.org/stdlib/libdoc/observer/rdoc/...

MenTaLguY

7/5/2007 10:26:00 PM

0

On Fri, 6 Jul 2007 07:13:31 +0900, "Dan Stevens (IAmAI)" <dan.stevens.iamai@gmail.com> wrote:
> I have a collection class that extends the Array class and I wish to
> have a particular method to be called whenever the contents of an
> instance of the class change. Can anyone advise on how I can achieve
> this?

For this purpose, it's probably better to wrap/delegate to Array than extend it.

That way, you can have control of all the methods that change something, and
guarantee that they will perform the appropriate notification. I'd also
recommend looking at Observer for the notification of the equation.

-mental


Dan Stevens (IAmAI)

7/5/2007 10:28:00 PM

0

> It sounds like you might be able to set up something like this with Observer.

Ah! I'd forgotten about the observer pattern. That should do the trick. Thanks!

On 05/07/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 7/5/07, Dan Stevens (IAmAI) <dan.stevens.iamai@gmail.com> wrote:
> > I have a collection class that extends the Array class and I wish to
> > have a particular method to be called whenever the contents of an
> > instance of the class change. Can anyone advise on how I can achieve
> > this?
> >
> > I'm aware that I can compare #hash with a previous saved value to
> > determine if the contents of the instance have changed since the last
> > time #hash was called. Therefore, if I can find a means of calling an
> > arbitrary method or piece of code every time any method is called on
> > the instance, I could achieve what I'm after. Can I override
> > Object#send?
>
> It sounds like you might be able to set up something like this with Observer.
> http://www.ruby-doc.org/stdlib/libdoc/observer/rdoc/...
>
>

Micah Martin

7/5/2007 10:58:00 PM

0

Great. Houlihans is quiet enough for us to talk. 5:00pm work for you?

Sent from my iPhone

On Jul 5, 2007, at 5:26 PM, MenTaLguY <mental@rydia.net> wrote:

> On Fri, 6 Jul 2007 07:13:31 +0900, "Dan Stevens (IAmAI)" <dan.stevens.iamai@gmail.com
> > wrote:
>> I have a collection class that extends the Array class and I wish to
>> have a particular method to be called whenever the contents of an
>> instance of the class change. Can anyone advise on how I can achieve
>> this?
>
> For this purpose, it's probably better to wrap/delegate to Array
> than extend it.
>
> That way, you can have control of all the methods that change
> something, and
> guarantee that they will perform the appropriate notification. I'd
> also
> recommend looking at Observer for the notification of the equation.
>
> -mental
>
>

dblack

7/5/2007 11:01:00 PM

0

James Gray

7/5/2007 11:04:00 PM

0

On Jul 5, 2007, at 5:57 PM, Micah Martin wrote:

> Great. Houlihans is quiet enough for us to talk. 5:00pm work for you?

We will be there!

Be sure to grab a big enough table to hold us. A few thousand will
be fine.

> Sent from my iPhone

Does it help you see _who_ you are sending messages to by chance? ;)

James Edward Gray II

Dan Stevens (IAmAI)

7/5/2007 11:04:00 PM

0

> Ah! I'd forgotten about the observer pattern. That should do the trick. Thanks!

After looking at the Observable module, it only really solves half of
my problem. While it's a nice mechanism for notify observers that an
object has changed, it does help me determine whether or not an object
has changed; it leaves that up to the programmer.

> For this purpose, it's probably better to wrap/delegate to Array than extend it.

This is a potential solution - I could override all the methods of the
Array class that I think modify the contents of the Array in a
superclass, calling Observable#changed before/after calling the
parent's method. However, what if I think there's one too many methods
to be overriding or a forget to override one?

Truth is, I've now realised that I actually don't *need* an answer to
this question to solve my problem (helps to have full insight into
one's problems first). However, for the sake of argument and my
curiosity, does anyone thing there's an easier and more reliable
solution?

On 05/07/07, MenTaLguY <mental@rydia.net> wrote:
> On Fri, 6 Jul 2007 07:13:31 +0900, "Dan Stevens (IAmAI)" <dan.stevens.iamai@gmail.com> wrote:
> > I have a collection class that extends the Array class and I wish to
> > have a particular method to be called whenever the contents of an
> > instance of the class change. Can anyone advise on how I can achieve
> > this?
>
> For this purpose, it's probably better to wrap/delegate to Array than extend it.
>
> That way, you can have control of all the methods that change something, and
> guarantee that they will perform the appropriate notification. I'd also
> recommend looking at Observer for the notification of the equation.
>
> -mental
>
>
>

dblack

7/5/2007 11:13:00 PM

0

Gregory Brown

7/5/2007 11:26:00 PM

0

On 7/5/07, Dan Stevens (IAmAI) <dan.stevens.iamai@gmail.com> wrote:
> > Ah! I'd forgotten about the observer pattern. That should do the trick. Thanks!
>
> After looking at the Observable module, it only really solves half of
> my problem. While it's a nice mechanism for notify observers that an
> object has changed, it does help me determine whether or not an object
> has changed; it leaves that up to the programmer.
>
> > For this purpose, it's probably better to wrap/delegate to Array than extend it.
>
> This is a potential solution - I could override all the methods of the
> Array class that I think modify the contents of the Array in a
> superclass, calling Observable#changed before/after calling the
> parent's method. However, what if I think there's one too many methods
> to be overriding or a forget to override one?

Um.... do you really need an observable array or are you building an
object that uses an array under the hood that needs to be observable?
Big difference there.

In the former case, you'll likely want to use a blank slate proxy that
does the notification on delegation. On the latter case it's really
easy, just notify on the methods you're actually using. :)

Word on the street is that subclassing core classes to change
behaviour is a bad idea anyway. You might risk your subclass methods
not getting called in favor of the C methods being called without you
knowing...

Gregory Brown

7/5/2007 11:30:00 PM

0

On 7/5/07, Micah Martin <micah@8thlight.com> wrote:
> Great. Houlihans is quiet enough for us to talk. 5:00pm work for you?

I changed my mind. Red Lobster is better.