File: emailtable2.rb

Project: Invitation to Ruby

#!/usr/bin/env ruby

require 'dbi'

Person = Struct.new(:first, :last, :nick, :email, :kind)
class Person
  def show
    printf "%-15s %-12s %s\n",
      "#{first} #{last}", "#{nick}/#{kind}", email
  end
end

def Person.from_hash(hash)
  params = ['first', 'last', 'nick', 'email', 'type'].
    collect {|n| hash[n]}
  self.new(*params)
end


db = DBI.connect ("DBI:Pg:jim", "jim")

sql = <<-SQL
SELECT first, last, nick, email, type
FROM person, email
WHERE person.personid = email.personid
ORDER BY nick, type
SQL

db.select_all(sql) { |row|
  p = Person.from_hash(row)
  p.show
}

db.disconnect


[ Index ][ Table of Contents ]
Generated by [ source2html ]