Youth culture killed my dog 9 to 13 of 22 articles Change bannerposts rsscomments rss

Stupid SQL tricks   13 Jan 05
[print link all ]

Consider the following tables:

people

id name
1 Jack
2 Jane
3 Raja
4 Aaliyah

questions

id text
1 What’s your favorite color?
2 Do you like dogs?

answers

person_id question_id value
1 1 Red
1 2 Yes
2 1 Blue
2 2 No
.. .. ..

This SQL:


SELECT people.id, MAX AS name,
MAX) AS favorite_color,
MAX) AS likes_dogs
FROM people, answers
WHERE people.id = answers.person_id
GROUP BY people.id

Will yield the following:
|. people.id|. name|. favoritecolor|. likesdogs|
|1|Jack|Red|Yes|
|2|Jane|Blue|No|
|3|Raja|Green|No|
|…|…|…|…|

A coworker of mine, Ken Gibbs, showed me this technique several years ago and I’ve used it frequently since. While this use of MAX and DECODE is hardly unknown , I have recently shown this to a few people who were really excited by it. So I thought I would share it with you, dear reader. Also, note that Oracle’s DECODE can easily be replaced with a CASE in other SQL dialects.

Update: fixed an error in the example SQL


|

Javascript, it's what's for dinner   25 Oct 04
[print link all ]

Javascript is a good language. I like Ruby better, but it is certainly better than Java (which is not a good language) and it has has certain unique charms. A dynamic, prototyped OO language with a runtime penetration of nearly 100% on desktop systems. I’m ashamed I haven’t done more with it.
This script from Florian Gross that adds several of the Ruby standard methods to Javascript is an excellent example how flexible and clean Javascript is. Below is a code snippit for adding Ruby style mixin support to Javascript. While it is impressive that Javascript is flexible enough to allow this sort of thing, the truly amazing bit is that it is seven lines of straightforward code that even a casual Javascripter such as myself can understand.

Object.prototype.extend = function(other) {
 if (!this.mixins) this.mixins = []
 this.mixins.push(other)
 for (var property in other)
   if (!this.hasOwnProperty(property))
     this[property] = other[property]
}

Now for the practical aspects of programming Javascript Ruby style.
Here’s some brief documentation written by Florian. Also while using ruby.js in a script that interacts with the DOM, I ran into a bit of trouble. In Firefox at least the Array that is returned by getElementsByTagName does not have the added methods. I would appreciate any explanation as to why this is so, but there is a work around in the mean time.
Instead of:


foo.getElementsByTagName(‘tr’).each(function(row) {
…do something with the row…
});

You have to:

Array.fromObject(foo.getElementsByTagName(‘tr’)).each(function(row) {
…do something with the row…
});


|

ImageMagick rules the Gimp   05 Sep 04
[print link all ]

I’ve always been jealous of graphic artists. They juggle dozens of layers and composite images with ease with their fancy photoshops and gimps. I’ve tried several times to do some simple tasks with the Gimp and was defeated in each case. So I’ve found myself going hat in hand to the “graphics guy” (no slight intended to the ladies; I’ve just never worked with a female graphic artist) for even the simplest of buttons and on personal projects I’ve just had to do without.

I’ve used ImageMagick occasionally over the years for converting between formats and checking image sizes. I recently stumbled on this great set of examples by Anthony Thyssen of using ImageMagick for complex multi-step manipulations. When you add in a terrific set of Ruby bindings, the result is graphical independence for people such as myself.

While I’ve still got a long way to go before I master these new techniques, I have used them to generate a banner for this page and a few buttons for a work project. Here’s the source of my banner generating script and you can try it out by clicking the Change banner link at the top of the page. Expect the banner to become ever more decedent as I add tricks to my tool box.


|

Greatest API design evar   02 Sep 04
[print link all ]

<pre> <steveny> Never do this: <steveny> Calendar calendar = Calendar.getInstance(Locale.US); <steveny> calendar.set(Calendar.MONTH, month); <steveny> calendar.set(Calendar.YEAR, year); <steveny> if it is the 31st now (the calendar.getInstance() returns the current time) <steveny> setting the month to a month with < 31 days <steveny> will cause the calendar be set to the 1st of the next month <darrend> niiiice <steveny> yep <richier> greatest API design evAR <steveny> so never do that ;) </pre>


|

XMLHttpRequest is your buddy   14 Aug 04
[print link all ]

I’ve added an autocomplete feature to liveUpdater.js that I’ve been harping on lately. The javascript is a bit more involved now, but I don’t think the Borges side of things has lost any of it’s charm.


  r.text_input_with_autocomplete(proc {}) do |text|
    if text.length > 0
      `grep -m 40 -i \"^#{text}\" /usr/share/dict/words`.split
    end
  end

I’ve set up a demo for your autocompleting pleasure. It seems to work ok on IE6 and Mozilla 1.7.2, but I’m sure it needs some work for other browsers. I would appreciate any patches that improve support for other browsers, fix bugs, or just clean up the javascript in general (I’m not quite a master of that language).


|

 

Copyright © 2024 Leslie A. Hensley