Jim Weirich wrote:
> I'm still unclear on how static methods do anything to support 
> meta-progarmming.  Perhaps an example might prove illuminating.

I am not sure this will be compelling enough, but here we go:

require 'dbi'

class Connection
	@@driver = 'DBI:ADO:Provider=SQLOLEDB;Data Source=...;Initial 
Catalog=...;User Id=...;Password=...;trusted_connection=yes'

	def initialize
		@dbh = DBI.connect(@@driver)
		@is_closed=false
	end

	def close
		@dbh.disconnect
		@is_closed=true
	end

	def closed?
		@is_closed
	end

	# MSSQL Server specific
	def columns(table_name)
		table_name = table_name.to_s if table_name.is_a?(Symbol)
		table_name = table_name.split('.')[-1] unless table_name.nil?
		sql = "SELECT COLUMN_NAME as ColName, COLUMN_DEFAULT as DefaultValue, 
DATA_TYPE as ColType, " +
			 "COL_LENGTH('#{table_name}', COLUMN_NAME) as Length, 
COLUMNPROPERTY(OBJECT_ID('#{table_name}'), " +
			 "COLUMN_NAME, 'IsIdentity') as IsIdentity, NUMERIC_SCALE as Scale " 
+
			 "FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '#{table_name}'"
		columns = []
		result = @dbh.select_all(sql)
		result.each { |field| columns << field }
		columns
	end
end

class MyClass
	# Static "constructor"
	def self.create(table)
		@@table = table
		@@connection = Connection.new

		columns = @@connection.columns(table)
		columns.each { |column|
		      property = 'fld'+column[0]
		      self.class_eval(%Q[
			def #{property}
			  @#{property}
			end
			def #{property}=(value)
			  @#{property} = value
			end
		      ])
		}
	end

	# Static "destructor"
	def self.destroy()
		@@connection.close unless @@connection.closed?
	end
end

MyClass.create('USERS')

table1 = MyClass.new
table2 = MyClass.new

table1.methods.each {|m| puts m if m =~ /^fld/ }

MyClass.destroy


-- 
Posted via http://www.ruby-forum.com/.