On 6/6/05, James Britt <james_b / neurogami.com> wrote: > Dominik Bathon wrote: > > First of all, this is no attempt to rival with James' nice Ruby Quiz ;-) > > > > I came up with a nice new(?) use for method_missing today. > > Now it is your job to figure out what you can do with it. > > (My hope is that someone will find nice uses for this, that I didn't > > think of) > > > > Interesting. > > > > > And some questions: > > > > What does it do? > > > > What can it be used for? > > > > Has this been done before? (I couldn't find anything.) > > > Reminds me of some code I wrote a few years ago when I was poking into > method-oriented programming. > > Rather than have > receiver.message( args ) > > I wanted to reverse things and do > message( args )->[ list_of_receivers ] > > > I hacked on Symbol and the result was that I could loop over a set of > objects, or use a proc to conditionally send the message to objects in > ObjectSpace (i.e., "I know what I want to do, I just don't know who to > do it to"). The results were then collected and returned as an array. > > The call looked something like > > :message.>>( obj_set_or_criteria_proc ) { args } > > I never found a really practical application for this. Conceivably, one > could close all open file handles, or shut down lingering socket > connections, or save user sessions if the session was x minutes old, or > whatever. > > It was mostly a "Gee, I wonder if ..." sort of thing, and the best > scenario I could think of was if I had an app that might need a global > shutdown, so one might want to dispatch a common set of messages across > a range of unknown objects before ending. > > I never released the code, being unhappy with the syntax. But I still > like the idea of casting a message out into object space and reeling > back the results. > > > BTW, I tried out your code, with a trivial example: > > p %w{ This is some text }.size > > Ah, but, of course, method_missing never gets called, so I did not get > what I wanted. > > This works, though: > > p %w{ This is some text }.upcase > > So there is the issue of trying to distribute a method across the list > when that method is also implemented by Array > > > > James > > -- > > http://www.ruby-doc.org - The Ruby Documentation Site > http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML > http://www.rubystuff.com - The Ruby Store for Ruby Stuff > http://www.jamesbritt.com - Playing with Better Toys > > I've thought sometimes that this would be a good shortcut: module Enumerable alias __original__map__ map def map(*args, &block) if args.length > 1 __original__map__ { |obj| args.inject(obj) { |o, meth_sym| o.send(meth_sym) } } else __original__map__(&block) end end end so we have [ ' hello ', ' world', ' ! '].map(:upcase, :strip) Doesn't give you anything over the block syntax, but it is slightly less typing.