Hi,
I thought to create a module to add an instance variable representing
the connection to a database:
module MyDbModule
attr :conn
require "postgres"
pghost="localhost"
pgport=nil
pgoptions=nil
pgtty=nil
dbname="mydb"
user='mylogin’
password=‘mypassword’
@conn=PGconn.connect(pghost,pgport,pgoptions,pgtty,dbname,user,password)
end
However, this doesn’t work. The Pickaxe describes modules as a group of
calsses, methods, constants, which my modules doens’t respect (no method
defined).
Is there a way to achieve what I’m trying, and in the case that it would
be possible, is it a good approach?
An alternative would be to put all code in a method and call this method
to initialise @conn. I’m wondering if I can get rid of the call to this
method though.
Thanks.
Raph
Bauduin Raphael wrote:
I thought to create a module to add an instance variable representing
the connection to a database:
module MyDbModule
class <<self
attr :conn
end
require "postgres"
pghost="localhost"
pgport=nil
pgoptions=nil
pgtty=nil
dbname="mydb"
user='mylogin'
password='mypassword'
@conn=PGconn.connect(pghost,pgport,pgoptions,pgtty,dbname,user,password)
end
However, this doesn’t work. The Pickaxe describes modules as a group of
calsses, methods, constants, which my modules doens’t respect (no method
defined).
The reason is that Module#attr creates accessor methods for the
instances of the module (in reality, the instances of classes including
the module). The @conn instance variable belongs to the MyDbModule
object itself, so attr needs to be called in the scope the class of
MyDbModule. It’s due to a metaclass hierachy headache.
Is there a way to achieve what I’m trying, and in the case that it would
be possible, is it a good approach?
I’d be more inclined to use singleton, and delay connecting to the
database until it is needed.
···
–
([ Kent Dahl ]/)_ ~ [ Kent Dahl - Kent Dahl ]/~
))_student_/(( _d L b_/ Master of Science in Technology )
( __õ|õ// ) ) Industrial economics and technology management (
_/ö____/ (_engineering.discipline=Computer::Technology)
Bauduin Raphael a écrit :
Hi,
I thought to create a module to add an instance variable representing
the connection to a database:
module MyDbModule
attr :conn
require “postgres”
pghost=“localhost”
pgport=nil
pgoptions=nil
pgtty=nil
dbname=“mydb”
user=‘mylogin’
password=‘mypassword’
@conn=PGconn.connect(pghost,pgport,pgoptions,pgtty,dbname,user,password)
end
To use a module variable through “MyModule.my_var”, just prefix the
variable name in the module with “self.” or “@@”.
Lio
Hi --
Bauduin Raphael a écrit :
> Hi,
>
> I thought to create a module to add an instance variable representing
> the connection to a database:
>
> module MyDbModule
> attr :conn
> require "postgres"
> pghost="localhost"
> pgport=nil
> pgoptions=nil
> pgtty=nil
> dbname="mydb"
> user='mylogin'
> password='mypassword'
>
> @conn=PGconn.connect(pghost,pgport,pgoptions,pgtty,dbname,user,password)
>
> end
To use a module variable through "MyModule.my_var", just prefix the
variable name in the module with "self." or "@@".
Not exactly; @@var is a class variable; self.var is a method call. If
you want an association between them, you have to write appropriate
methods:
class X
def self.my_var
@@my_var
end
# etc.
end
David
···
On Sun, 4 Apr 2004, Lionel Thiry wrote:
--
David A. Black
dblack@wobblini.net
http://segment7.net/projects/ruby/snippets/database.rb
Is an example of this approach.
···
Kent Dahl (kentda+news@stud.ntnu.no) wrote:
Is there a way to achieve what I’m trying, and in the case that it would
be possible, is it a good approach?
I’d be more inclined to use singleton, and delay connecting to the
database until it is needed.
–
Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04