From: Dave Thomas Date: 2001-11-30T08:04:50+09:00 Subject: [ruby-talk:26992] Re: Can someone explain TupleSpaces? ptkwt@shell1.aracnet.com (Phil Tomson) writes: > First of all, what is the Tuple? It's a slightly bad name :) A tuple is an array of values, capable of being stored in tuplespace. A tuplespace is a (potentially distributed) big bag-o-tuples. A tuplespace has three basic operations: - out: store a tuple - in: get a tuple (removing it from the tuplespace) - rd: get a tuple, leaving it in the tuplespace The magic happens in the 'in' and 'rd' methods, because they perform pattern matching: just like reading a row from a database, you can specify a pattern, and the tuplespace will return some arbitrary row that matches the pattern (waiting until one does it there's not one i the tuplespace). So, say we have a communal tuplespace that records what mp3 each of us is listening to. I could store the track title, indexed under my name, using something like: ts.out [ 'dave', track ] Someone 10,000 miles away could find out what I was listening to using name, track = ts.rd [ 'dave', nil ] The pattern here is the parameter to #rd: it's a template for the tuples I'm prepared to accept back from the read, in this case all those with 'dave' as the first value and an arbitrary second parameter. Now, when my mp3 player changes tracks, I want to update my listing. I could do something like: ts.in [ 'dave', nil ] # delete old entry ts.out [ 'dave', track ] # add new one Typically, though, you implement higher level protocols on top of tuplespaces using these basic operations. One of the neat things about Ruby's implementation of tuplespaces is that the matching of tuples is done using an element-by-element test using the === operator. That means you can do things like ts.rd [ /^D/, nil ] # read a tuple for someone whose name starts # with a D ts.rd [ nil, JazzSong ] # return a tuple that has a JazzSong # object in the second parameter (and, abandoning the mp3 tuplespace) ts.rd [ nil, 21..35, :male, /usa|canada/i ] # return an entry for a person whose age is # between 21 and 35, who is male, and # whose address is in canada or the us. Fun, eh? Dave