Bret Pettichord wrote: > At 08:18 PM 8/18/2005, Jamey Cribbs wrote: > >> Hal convinced me to rewrite KirbyBase and allow the user to specify >> query syntax using blocks. When I finally figured out how easy it >> was to add this functionality and how much power it gave the user to >> specify queries, I was sold. > > > Could you show us an example of this? I'm sure i should be using > blocks more myself and i'd like to see how others are doing it. Ok, here is a somewhat simplified code example. Let's say you have a table that holds information about WWII airplanes. You want to write a query that selects all planes from the table that belonged to the US and had a top speed greater than 350mph. First you would write the query, using a block to specify the actual select condition: results = plane_tbl.select { |r| r.country == 'USA' and r.speed > 350 } Notice that Ruby allows you to easily pass around code that keeps it's local context, you can simply write the query using Ruby, instead of having to, say, write a string that KirbyBase would have to do an #eval on to figure out what you want to do. Now, how is this query we wrote above handled? Here is a greatly simplified version of KirbyBase's select method: def select(&select_cond) result = [] @db.engine.get_recs(self).each do |rec| result << rec if select_cond.call(rec) end return result end Notice that the query block is passed in and assigned to the variable select_cond. Then, KirbyBase loops through all the records in the table. For each record it executes the block, passing in the values of the current record to the block. If the return value from the block is true, then the record is added to the result set. After all records have been looped through, the result set is returned to user. Before I re-wrote KirbyBase to use blocks, I had written an earlier version where the user specifed their query by putting it inside a string. The select method then parsed the string and built up the query itself. It was tedious ugly code, parsing the string, trying to make sure I covered all the ways the user might specify the query string. Additionally, the user was very limited in the kinds of comparisons they could do in the query string. Basically, if I the parsing code in the select method could not handle it, they couldn't do it. Then, when I switched KirbyBase to handle blocks, that totally changed. Now, the user has total freedom to write their query the way they want, because, as long as their query is valid Ruby code, it will work! Even better, if Matz adds new classes or methods to Ruby, I don't need to change a line of code of KirbyBase to accomodate it, because, when KirbyBase executes a query, it is actually Ruby that is doing all the heavy lifting, since the query is simply Ruby code itself. This totally sold me on blocks. :-) Jamey Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.