[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

A long call chain...

benjohn

3/21/2006 6:48:00 AM


Hello everyone. I'm hoping to do something like this (and then
automate it up by putting something neat in to a mix in module)...

class Test
def xxx; [1]; end
alias xxx_old xxx
def xxx; xxx_old + [2]; end
alias xxx_old xxx
def xxx; xxx_old + [3]; end
alias xxx_old xxx
...
end

In real use, these various defs and aliases would be in different
files, but would often be putting things in to the same class. The
above doesn't work - I think it's leading to a non terminating
recursive function.

My first question is, "is it correct that this doesn't work?"


I'd like to use a mechanism like this to enable me to have a kind of
hookable function in a class. As I said, the alias process would be
encapsulated by waving my hands and working that out later, but
probably making use of method_defined.

The mechanism allows you to build up an event handler: you send an
event to a class instance, and any receivers that are registered for
that event (by having defined the method to pick it up) will get it.
Example hookable methods for my system will be: is_being_setup,
will_be_torn_down, has_recieved_message will_send_message.

It's worth noting that the will_be methods should probably be
executed in the reverse order from that in which they are defined!

I'm pretty sure this is a good plan, but...

Question: "Does this seem like a good plan?"


Finally, as this doesn't work, can you see a nicer way to implement
it? I think I could capture the function each time it gets defined,
and put it in to class state, but it would need to be per class, I
think, rather than just on in the base class. I've thought about
several variations, but none of them seem to be very nice at all.


Thank you for any thoughts you have,
Cheers,
Benjohn


4 Answers

Eric Hodel

3/21/2006 7:36:00 AM

0

On Mar 20, 2006, at 10:48 PM, Benjohn Barnes wrote:

> Hello everyone. I'm hoping to do something like this (and then
> automate it up by putting something neat in to a mix in module)...
>
> class Test
> def xxx; [1]; end
> alias xxx_old xxx
> def xxx; xxx_old + [2]; end
> alias xxx_old xxx
> def xxx; xxx_old + [3]; end
> alias xxx_old xxx
> ...
> end

Yuck.

> In real use, these various defs and aliases would be in different
> files, but would often be putting things in to the same class. The
> above doesn't work - I think it's leading to a non terminating
> recursive function.
>
> My first question is, "is it correct that this doesn't work?"

The fact that this is unmaintainably difficult to do is a very, very
big clue. Note also that this will break if you change the order of
require, not good.

> I'd like to use a mechanism like this to enable me to have a kind
> of hookable function in a class. As I said, the alias process would
> be encapsulated by waving my hands and working that out later, but
> probably making use of method_defined.
>
> The mechanism allows you to build up an event handler: you send an
> event to a class instance, and any receivers that are registered
> for that event (by having defined the method to pick it up) will
> get it. Example hookable methods for my system will be:
> is_being_setup, will_be_torn_down, has_recieved_message
> will_send_message.

observer.rb provides something similar to what you want, event
notification.

> It's worth noting that the will_be methods should probably be
> executed in the reverse order from that in which they are defined!
>
> I'm pretty sure this is a good plan, but...
>
> Question: "Does this seem like a good plan?"

Not with random aliasing it doesn't.

> Finally, as this doesn't work, can you see a nicer way to implement
> it? I think I could capture the function each time it gets defined,
> and put it in to class state, but it would need to be per class, I
> think, rather than just on in the base class. I've thought about
> several variations, but none of them seem to be very nice at all.

Check out MuffDaddy:

http://blog.zenspider.com/archives/2005/02/muffdaddy_...

What do you want to use this for? Why?

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...




benjohn

3/21/2006 9:23:00 AM

0

This morning, I asked about using aliases and wrote:
*snip*
> I'd like to use a mechanism like this to enable me to have a kind of
> hookable function in a class. As I said, the alias process would be
> encapsulated by waving my hands and working that out later, but
> probably making use of method_defined.
>
> The mechanism allows you to build up an event handler: you send an
> event to a class instance, and any receivers that are registered for
> that event (by having defined the method to pick it up) will get it.
> Example hookable methods for my system will be: is_being_setup,
> will_be_torn_down, has_recieved_message will_send_message.
>
> It's worth noting that the will_be methods should probably be
> executed in the reverse order from that in which they are defined!
>
> I'm pretty sure this is a good plan, but...
>
> Question: "Does this seem like a good plan?"

Well, I still think the idea in general is good, but I've really gone
off the interface I was proposing. I think it's going to be hard to
implement anyway, and it's a bit unexpected to def a method, but have
really odd behaviour happen for no obviously visible reason...

> Finally, as this doesn't work, can you see a nicer way to implement
> it? I think I could capture the function each time it gets defined,
> and put it in to class state, but it would need to be per class, I
> think, rather than just on in the base class. I've thought about
> several variations, but none of them seem to be very nice at all.

The train on the way here got me thinking of another way. I now think a
good approach is to add a module method "add_to_hook" that can be used
thus:

class Blah
include Hook

def this_should_happen_on_hook_start_up; end
def this_should_also_happen_on_hook_start_up; end

add_to_hool :start_up, :this_should_happen_on_hook_start_up,
:this_should_also_happen_on_hook_start_up
end

And also add instance methods to support:
Blah.new.call_hook( :start_up )
Blah.new.call_hook_backwards( :tear_down )

The methods call_hook and call_hook_backwards will look for hooks added
in the receiver's class, and also it's superclasses (I'm going to leave
out any included modules for now, but I think that would also be fine).

The methods to be called will be despatched to as "normal" using the
instance method send. This allows them to be overidden in the normal
way, which could be useful.

I've got some tests on my laptop, and the start of an implementation
(doesn't do any looking in the superclass yet); I'll post them up, but
can't do so from work because of security paranoia :)

Two further things though:

* I don't like the name "hook" much, because there are lots of hooks
in to a hookable, really. Which is verging on being decidedly HP
Lovecraft. But "event" and "snooper" just sound weak.

* TDD rocks!




benjohn

3/23/2006 10:59:00 PM

0


On 21 Mar 2006, at 07:35, Eric Hodel wrote:

> On Mar 20, 2006, at 10:48 PM, Benjohn Barnes wrote:
>
>> Hello everyone. I'm hoping to do something like this (and then
>> automate it up by putting something neat in to a mix in module)...
>>
>> class Test
>> def xxx; [1]; end
>> alias xxx_old xxx
>> def xxx; xxx_old + [2]; end
>> alias xxx_old xxx
>> def xxx; xxx_old + [3]; end
>> alias xxx_old xxx
>> ...
>> end
>
> Yuck.

:) Yeah, sorry about that.

*snip*

>> I'd like to use a mechanism like this to enable me to have a kind
>> of hookable function in a class. As I said, the alias process
>> would be encapsulated by waving my hands and working that out
>> later, but probably making use of method_defined.
>>
>> The mechanism allows you to build up an event handler: you send an
>> event to a class instance, and any receivers that are registered
>> for that event (by having defined the method to pick it up) will
>> get it. Example hookable methods for my system will be:
>> is_being_setup, will_be_torn_down, has_recieved_message
>> will_send_message.
>
> observer.rb provides something similar to what you want, event
> notification.

*nods* It's extremely close to the observer pattern.

I wanted to build a very modular, and very easy to extend
application. I was trying to use Ruby's open classes to pull lots of
extra things in to a class. What I noticed was that generally, the
observers of a message sent by an object x, were in fact other parts
of the object x. I was hoping to make use of this fact to avoid
needing to subscribe things to a broadcaster.

However, I'm pretty sure now that I was going in a bad direction, and
instead, these bolt on / plug in features should be a system of
collaborating objects, rather than a monolithic whole. This will also
make subsystems much more testable too, I think. What I'd like
though, is to avoid needing to unnecessarily describe how things
should get stitched together.

Anyway - I've just seen mention of Needle in the ruby quiz summary.
It sounds distinctly close to the kind of things I'm thinking about,
and the gem's just finished pulling itself down, so I'll go and have
a look.

Cheers,
Benjohn



usenet

9/26/2012 8:25:00 PM

0

Dr. Jai Maharaj posted:
>
> In article
> <2c556148-67f5-4050-bc1f-9f8d07553ae3@googlegroups.com>,
> fanabba <fana...@aol.com> posted:
> >
> > Dr. Jai Maharaj posted:
> >
> > I posted the following in 1993:
> >
> > [ From: Dr. Jai Maharaj
> > [ Subject: * Understanding Hinduism *
> > [ Newsgroups: soc.culture.indian
> > [ Date: 7 Sep 1993
> >
> > Understanding Hinduism
> >
> > Excerpts from
> > HINDUISM by Troy Wilson Organ
> > Distinguished Professor of Philosophy
> > Ohio University, Athens, Ohio
> > Barron's Educational Series, New York, 1974
> >
> > Hinduism is a theonomous culture. A theonomous culture is
> > one in which the superior law is at the same time the
> > innermost law of man himself. Hinduism as a religion and
> > Hinduism as a culture are so intertwined that we can
> > never be sure whether a certain mode of behavior is Hindu
> > or Indian.
> >
> > We can never be sure whether a way of behaving is
> > prescriptively right or descriptively correct, whether it
> > is normative or traditional. Perhaps this is the penalty
> > and the premium of a long unbroken tradition.
> >
> > The customary and the moral, the cultural and the
> > religious, are no longer discernible. Tillich says,
> > "Religion is the substance of culture and culture the
> > form of religion." (1)
> >
> > We are tempted to alter this for our purposes to read:
> > "Hindu religion is the substance of Indian culture and
> > Indian culture the form of Hindu religion,"
> >
> > Hinduism, in other words, has been such a pervasive
> > element in the life of the people of South Asia that the
> > disentanglement of religion and culture is a risky
> > undertaking.
> >
> > For example, the caste mentality, which is surely Hindu
> > in origin, has permeated the thinking of all peoples of
> > India. Muslims in India divide themselves into Sayad,
> > Mughal, Sheikh, and Pathan; Zoroastrians distinguish
> > Atharva, Rathaestha, Vastryafshuyan, and Huiti; and
> > Christians and Jews make similar distinctions. Some
> > Christians in Madras state even boast that they hold more
> > firmly to the caste system than do Hindus. (2)
> >
> > Since Hinduism is not easily divided into the sacred and
> > the profane, a Hindu will have difficulty in interpreting
> > his religion to a Westerner. Sanskrit has no word for
> > religion. Words like "devotion" (bhakti), "duty" (dharm),
> > and "discipline" (yog) have to suffice.
> >
> > Footnotes
> >
> > (1) Paul Tillich, "The Protestant Era." Third impression.
> > Chicago: University of Chicago Press, 1960, page 57.
> >
> > (2) L. K. A. Iyer, "Anthropology of the Syrian
> > Christians." Ernakulam, 1926, page 216. J. H. Hutton,
> > "Caste in India." Fourth edition. London: Oxford
> > University Press, 1963, page 121.
> >
> > End of excerpts
> >
> > Jai Maharaj, Jyotishi
> > Om Shanti
> >
> > http://groups.google.com/group/alt.fan.j...
> > Dhanyavaad for your post !
>
> You're welcome.
>
> > Sanaatan Dharm Kee Jai Ho !
> > Dhanyavaad for your service to Sanaatan Dharm !
>
> Dhanyavaad for the encouragement and your service to
> Sanaatan Dharm!
>
> Sanaatan Dharm Kee Jai Ho!
>
> Jai Maharaj, Jyotishi
> Om Shanti
>
> http://groups.google.com/group/alt.fan.j...

In article
<e1553140-78d9-4623-a3b8-151960e41751@googlegroups.com>,
fanabba <fana...@aol.com> posted:
>
> Hindus should not fall for Christian Deceit.
>
> Christianity is exclusivist and supremacist.
>
> Hindus being targeted for conversion by Christian
> Missionaries and Evangelists must be made aware of the
> atrocities inflicted on Hindus by Christians. The Goa
> Inquisition (
> http://en.wikipedia.org/wiki/Goa_I... ) is but one
> example.
>
> Hindus should also be informed of the genocide of native
> Americans by Christians, and the enslavement of black
> Africans and the Rapa Nui on Easter Island by Christians.
>
> Hindus being targeted for conversion by Christian
> Missionaries and Evangelists must be made aware of
> Christian imperialism in the whole world.
>
> Christian conversion campaigns are expressions of
> Christian Supremacism, Christian Intolerance and
> Christian Ill-intent, which are caused by Christian
> Ignorance and Christian Arrogance.
>
> Hindus should not fall for Christian Deceit.
>
> Christianity is exclusivist and supremacist.
>
> The Goa Inquisition
> http://www.christianaggression.org/item_display.php?type=articles&id=...
>
> Christian Aggression
> http://christianaggr...

Dhanyavaad for your post and your service to Sanaatan Dharm!

Sanaatan Dharm Kee Jai Ho!

Jai Maharaj, Jyotishi
Om Shanti

http://groups.google.com/group/alt.fan.j...