From: Kent Dahl Date: 2004-04-04T23:45:34+09:00 Subject: Re: good module use? Bauduin Raphael wrote: > I thought to create a module to add an instance variable representing > the connection to a database: > > module MyDbModule class < 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 ]/)_ _~_ _____[ http://www.pvv.org/~kentda/ ]_____/~ ))\_student_/(( \__d L b__/ Master of Science in Technology ) ( \__\_�|�_/__/ ) _) Industrial economics and technology management ( \____/_�_\____/ (____engineering.discipline_=_Computer::Technology___)