Hi --
I'm looking for the best method of coding. I have module. "module
Standards." It consists of production standard constants and methods
that will be called from all over the place. It is gem'd, and is
implemented with a require method. I would like to change some of the
constants over to accessor variables, so I can change certain settings
on the fly (for testing purposes.) I would like the accessor variable
to have a default value, but I don't know to accomplish this. (Can I
even have accessor variables/methods in a module?) I would like to get
things as simple as this:
attr :lpClientDrive, true # like to have default value here
...
def lpClientDrive=(dr)
@lpClientDrive = dr
end
You don't need that setter method, as the "true" flag will cause both
reader and writer methods to be created. (The "true" flag is
considered a bit obscure; it's more usual to use attr_accessor.)
What I currently have (without ability to update) is
def lpClientDrive; "//server/folder/foo/"; end
For testing purposes, I would like to re assign the client drive, as
testing will be done on a non-production machine.
It sounds like what you want, in essence, is:
module Standards
def lpClientDrive
@lpClientDrive || "//server/folder/foo"
end
def lpClientDrive=(dr)
@lpClientDriver ||= dr
end
end
You could write a nice attr_*-style method to generate these. Here's
an example:
module AttrExtensions
def attr_with_default(attr,default)
attr_writer attr
define_method(attr) do
instance_variable_get("@#{attr}") || default
end
end
end
module MyModule
extend AttrExtensions
attr_with_default("x", 100)
end
class C
include MyModule
end
c = C.new
p c.x # 100 (the default)
c.x = 200
p c.x # 200 (the newly-assigned value)
David
···
On Tue, 28 Nov 2006, donn@cmscms.com wrote:
--
David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org