D
David Holroyd
[...] static destructors (something I always wanted to have in
Java/C#).
A bit off-topic, but note,
http://java.sun.com/docs/books/vmspec/html/Concepts.doc.html#24377
dave
[...] static destructors (something I always wanted to have in
Java/C#).
I believe this is the case if the garbage collector never runs. That is,
if the program exits before it is necessary.
Also, there are situations in which objects are never released, so the
finalize method won't be called then, either. If I recall correctly.
I believe this is the case if the garbage collector never runs. That = is,=20
if the program exits before it is necessary.
Also, there are situations in which objects are never released, so the =
finalize method won't be called then, either. If I recall correctly.
Jim said:I'm still unclear on how static methods do anything to support
meta-progarmming. Perhaps an example might prove illuminating.
Primary said:Jim said: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:ADOrovider=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
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.