good module use?

  • Thread starter Bauduin Raphael
  • Start date
B

Bauduin Raphael

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
 
K

Kent Dahl

Bauduin said:
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.
 
L

Lionel Thiry

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
 
D

David A. Black

Hi --

Bauduin Raphael a écrit :

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
 
L

Lionel Thiry

David A. Black a écrit :
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:
Oops, wrote my advice too fast! ;)
Well, I've found how I could write such a mistake... Learning from
mistakes is always a good thing, isn't it? (but teaching them is not,
sorry!)

Lio
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top