# Module, requires, and attr

**URL:** <https://www.ruby-forum.com/t/module-requires-and-attr/77438>\
**Category:** Ruby\
**Created:** [November 28, 2006, 12:41am UTC](https://www.ruby-forum.com/t/module-requires-and-attr/77438 "2006-11-28T00:41:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 28, 2006, 12:41am UTC](https://www.ruby-forum.com/t/module-requires-and-attr/77438/1 "2006-11-28T00:41:14Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 28, 2006, 1:03am UTC](https://www.ruby-forum.com/t/module-requires-and-attr/77438/2 "2006-11-28T01:03:16Z")

</div>

Hi –

On Tue, 28 Nov 2006, removed\_email\_address@domain.invalid wrote:

> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 28, 2006, 1:09am UTC](https://www.ruby-forum.com/t/module-requires-and-attr/77438/3 "2006-11-28T01:09:16Z")

</div>

Hi –

On Tue, 28 Nov 2006, removed\_email\_address@domain.invalid wrote:

> def lpClientDrive=(dr)  
> @lpClientDriver ||= dr

Ignore the || there; I meant just = .

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 28, 2006, 1:31am UTC](https://www.ruby-forum.com/t/module-requires-and-attr/77438/4 "2006-11-28T01:31:20Z")

</div>

Thank you very much. This is the kind of information I need to learn.  
Thanks for everything.  
dvn
