# Attributes-3.2.0

**URL:** <https://www.ruby-forum.com/t/attributes-3-2-0/72421>\
**Category:** Ruby\
**Created:** [October 17, 2006, 6:23am UTC](https://www.ruby-forum.com/t/attributes-3-2-0/72421 "2006-10-17T06:23:03Z")\
**Posts on this page:** 8\
**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:** [October 17, 2006, 6:23am UTC](https://www.ruby-forum.com/t/attributes-3-2-0/72421/1 "2006-10-17T06:23:03Z")

</div>

NAME

attributes.rb

URIS

[http://rubyforge.org/projects/codeforpeople/](http://rubyforge.org/projects/codeforpeople/)  
[http://codeforpeople.com/lib/ruby](http://codeforpeople.com/lib/ruby)

SYNOPSIS

attributes.rb provides an attr\_\* like method will several user  
friendly  
additions. attributes.rb is similar to the traits.rb package but  
sacrafices  
a few features for simplicity of implementation: attributes.rb is  
only 42  
lines of code.

the implimentation of attributes.rb borrows many of the best ideas  
from the  
metakoans.rb ruby quiz

```
 http://www.rubyquiz.com/quiz67.html

```

in particular the solutions of Christian N. and Florian  
Gross.

SAMPLES

\<========\< samples/a.rb \>========\>

~ \> cat samples/a.rb

```
 #
 # basic usage is like attr, but note that attribute defines three 

```

methods,  
# getter, setter, and query  
#  
require ‘attributes’

```
   class C
     attribute 'a'
   end

   c = C.new

   c.a = 42 # setter
   p c.a # getter
   p 'forty-two' if c.a? # query

 #
 # also not that attribute(s) works as well for objects as classes
 #
   o = Object.new
   o.attribute 'answer' => 42
   p o.answer

```

~ \> ruby samples/a.rb

```
 42
 "forty-two"
 42

```

\<========\< samples/b.rb \>========\>

~ \> cat samples/b.rb

```
 #
 # default values may be given either directly or as a block which 

```

will be  
# evaluated in the context of self. in both cases (value or block)  
the  
# default is set only once and only if needed - it’s a lazy  
evaluation.  
#  
require ‘attributes’

```
   class C
     attribute :a => 42
     attribute(:b){ Float a }
   end

   c = C.new

   p c.a
   p c.b

```

~ \> ruby samples/b.rb

```
 42
 42.0

```

\<========\< samples/c.rb \>========\>

~ \> cat samples/c.rb

```
 #
 # multiple values may by given, plain names and key/val pairs may 

```

be mixed.  
#  
require ‘attributes’

```
   class C
     attributes 'x', 'y' => 0b101000, 'z' => 0b10
   end

   c = C.new
   c.x = c.y + c.z
   p c.x

```

~ \> ruby samples/c.rb

```
 42

```

\<========\< samples/d.rb \>========\>

~ \> cat samples/d.rb

```
 #
 # a nice feature is that all attributes are enumerated in the 

```

class. this,  
# combined with the fact that the getter method is defined so as to  
delegate  
# to the setter when an argument is given, means bulk  
initialization and/or  
# attribute traversal is very easy.  
#  
require ‘attributes’

```
   class C
     attributes %w( x y z )

     def attributes
       self.class.attributes
     end

     def initialize
       attributes.each_with_index{|a,i| send a, i}
     end

     def to_hash
       attributes.inject({}){|h,a| h.update a => send(a)}
     end

     def inspect
       to_hash.inspect
     end
   end

   c = C.new
   p c.attributes
   p c

   c.x 'forty-two'
   p c.x

```

~ \> ruby samples/d.rb

```
 ["x", "y", "z"]
 {"x"=>0, "y"=>1, "z"=>2}
 "forty-two"

```

\<========\< samples/e.rb \>========\>

~ \> cat samples/e.rb

```
 #
 # my favourite element of attributes is that getters can also be 

```

setters.  
# this allows incredibly clean looking code like  
#  
require ‘attributes’

```
   class Config
     attributes %w( host port)
     def initialize(&block) instance_eval &block end
   end

   conf = Config.new{
     host 'codeforpeople.org'

     port 80
   }

   p conf

```

~ \> ruby samples/e.rb

```
 #<Config:0xb75cc6bc @port=80, @host="codeforpeople.org">

```

enjoy.

-a

---

<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:** [October 17, 2006, 1:01pm UTC](https://www.ruby-forum.com/t/attributes-3-2-0/72421/2 "2006-10-17T13:01:10Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> NAME
> 
> attributes.rb

with :

~/Desktop%\> sudo gem install attributes

i get :  
ERROR: Error installing gem attributes[.gem]: undefined method  
`required\_ruby\_version’ for nil:NilClass

over MacOS X 1O.4.8

and :

> ruby -version  
> ruby 1.8.5 (2006-08-25) [powerpc-darwin8.8.0]

and :  
~/Desktop%\> gem -v  
0.9.0

---

<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:** [October 17, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/attributes-3-2-0/72421/3 "2006-10-17T15:23:30Z")

</div>

Une Bévue wrote:

> ERROR: Error installing gem attributes[.gem]: undefined method  
> `required\_ruby\_version’ for nil:NilClass

I’m also on Mac OS X 10.4.8, and it works for me, but with an older  
version of gems:

Slim:~ gavinkistner$ ruby -v && gem -v  
ruby 1.8.5 (2006-08-25) [powerpc-darwin8.7.0]  
0.8.11  
Slim:~ gavinkistner$ sudo gem install attributes  
Password:  
Attempting local installation of ‘attributes’  
Local gem file not found: attributes\*.gem  
Attempting remote installation of ‘attributes’  
Updating Gem source index for: [http://gems.rubyforge.org](http://gems.rubyforge.org)  
Successfully installed attributes-3.2.0

In fact, I can’t seem to upgrade gems (but let’s not hijack the thread  
with that bit of info).

---

<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:** [October 17, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/attributes-3-2-0/72421/4 "2006-10-17T16:05:22Z")

</div>

Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In fact, I can’t seem to upgrade gems (but let’s not hijack the thread  
> with that bit of info).

i can’y downgrade )))

but i think i could install it “manually” by myself, don’t worry 😉

---

<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:** [October 17, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/attributes-3-2-0/72421/5 "2006-10-17T16:31:52Z")

</div>

On 17-Oct-06, at 7:00 AM, Une Bévue wrote:

> and :  
> ~/Desktop%\> gem -v  
> 0.9.0

No problem here and I have almost the same configuration (I have i686-  
darwin8.8.1)

Cheers,  
Bob

* * *

Bob H. – blogs at \<[http://www.recursive.ca/](http://www.recursive.ca/)  
hutch/\>  
Recursive Design Inc. – [http://www.recursive.ca/](http://www.recursive.ca/)  
Raconteur – [http://www.raconteur.info/](http://www.raconteur.info/)  
xampl for Ruby – [http://rubyforge.org/projects/xampl/](http://rubyforge.org/projects/xampl/)

---

<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:** [October 17, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/attributes-3-2-0/72421/6 "2006-10-17T17:47:14Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> try to get a backtrace and send to the gem team.

i’d like (i’m also a gem modest designer) to help the gem community but  
i don’t know how to do a backtrace…

i get about the same prob attempting a remote install :

~/Desktop%\> sudo gem install -r attributes  
Password:  
Bulk updating Gem source index for: [http://gems.rubyforge.org](http://gems.rubyforge.org)  
ERROR: While executing gem … (NoMethodError)  
undefined method `each’ for #Object:0x67518d0

---

<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:** [October 17, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/attributes-3-2-0/72421/7 "2006-10-17T16:15:33Z")

</div>

On Tue, 17 Oct 2006, [ISO-8859-1] Une Bévue wrote:

> and :  
> ~/Desktop%\> gem -v  
> 0.9.0

sorry, must be on your end mac/ruby/gem:

harp:~ \> gem install attributes  
[]  
Attempting local installation of ‘attributes’  
Local gem file not found: attributes\*.gem  
Attempting remote installation of ‘attributes’  
Updating Gem source index for: [http://gems.rubyforge.org](http://gems.rubyforge.org)  
Successfully installed attributes-3.2.0

harp:~ \> ruby -v  
ruby 1.8.4 (2005-12-01) [i686-linux]

harp:~ \> gem -v  
0.8.11

try to get a backtrace and send to the gem team.

cheers.

-a

---

<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:** [October 17, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/attributes-3-2-0/72421/8 "2006-10-17T18:25:27Z")

</div>

On Wed, 18 Oct 2006, [ISO-8859-1] Une Bévue wrote:

> i get about the same prob attempting a remote install :
> 
> ~/Desktop%\> sudo gem install -r attributes  
> Password:  
> Bulk updating Gem source index for: [http://gems.rubyforge.org](http://gems.rubyforge.org)  
> ERROR: While executing gem … (NoMethodError)  
> undefined method `each’ for #Object:0x67518d0

try

~/Desktop%\> sudo gem --debug install -r attributes

-a
