On Mon, 3 Mar 2003, Brian Candler wrote:

> You might find that one of the following patterns achieves what you want
> without too much difficultly:
>
>   <do something> unless foo.nil?  # shorter than 'foo.instance_of? NilClass'
>
>   foo ||= SomeDefaultValue        # only assigns if foo==nil or false
>
> Or you could use begin...rescue...end around the code section.

i often use

eg.

  user    = ENV['USER']
  comment = 'foobar'
  date    = nil

  statements = <<-sql
    insert into
      mytable
	values ( #{user}, #{comment}, #{date or Time.now} )
  sql

  conn.exec statements

but better still

  create table mytable
    (
      user text,
      comment text,
      date timestamp default now()
    )

since this will handle 'nil' values from ruby, perl, c, sql-command prompt,
etc.

IMHO: if there is the notion of a 'default' date for your schema, it should be
represented in the schema - not ruby code.  if there is not, the TypeError
raised should halt the program so date <=> nil need not be changed.  of
course, this is not always feasible but i thought i'd mention it...

-a

--
  ====================================
  | Ara Howard
  | NOAA Forecast Systems Laboratory
  | Information and Technology Services
  | Data Systems Group
  | R/FST 325 Broadway
  | Boulder, CO 80305-3328
  | Email: ahoward / fsl.noaa.gov
  | Phone:  303-497-7238
  | Fax:    303-497-7259
  ====================================