# Generate accessors for an hash

**URL:** <https://www.ruby-forum.com/t/generate-accessors-for-an-hash/143865>\
**Category:** Ruby\
**Created:** [July 23, 2008, 11:41am UTC](https://www.ruby-forum.com/t/generate-accessors-for-an-hash/143865 "2008-07-23T11:41:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bcef8e/32.png) [@andre83](https://www.ruby-forum.com/u/andre83)\
**Post date:** [July 23, 2008, 11:41am UTC](https://www.ruby-forum.com/t/generate-accessors-for-an-hash/143865/1 "2008-07-23T11:41:42Z")

</div>

Hello, I need to generate read and write accessors for all the keys in  
an hash.  
Say I have an attribute named my\_attribute, I need “my\_attribute” and  
“my\_attribute=” methods to read and write it to  
@attributes[my\_attribute].

I tried this code in the class constructor:

```
for a in all_attributes
  method_name = a.name
  self.class.class_eval do
    define_method method_name do
      @attributes[method_name]
    end
    define_method method_name+"=" do |val|
      @attributes[method_name] = val
    end
  end
end

```

It generates the method for the class, but all the methods read and  
write to the last attribute in the all\_attributes list. That’s  
strange…  
Thank you,

Andrea Gianarro

---

<div class="post-metadata">

**Author:** ![andre83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bcef8e/32.png) [@andre83](https://www.ruby-forum.com/u/andre83)\
**Post date:** [July 23, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/generate-accessors-for-an-hash/143865/2 "2008-07-23T12:30:23Z")

</div>

> ```
> @attributes[method_name] = val
> end
> end
> 
> ```
> 
> end
> 
> It generates the method for the class, but all the methods read and  
> write to the last attribute in the all\_attributes list. That’s  
> strange…

Ah, the strange joy that is closures. Basically, those ‘define\_method’  
calls  
allow the code block to retain access to whatever variables were in  
scope  
when the methods are defined. So, when those methods actually run, the  
variable ‘method\_name’ will have the last value in the loop – each  
closure  
references a single variable, it does not copy its value.

You might be better off with method\_missing

class Hash  
def method\_missing(name, value = nil)  
name = name.to\_s  
if name =~ /=$/  
self[name.sub(/=$/, ‘’)] = value  
else  
return self[name]  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![andre83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bcef8e/32.png) [@andre83](https://www.ruby-forum.com/u/andre83)\
**Post date:** [July 23, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/generate-accessors-for-an-hash/143865/3 "2008-07-23T15:08:44Z")

</div>

Andrea Gianarro wrote:

> Hello, I need to generate read and write accessors for all the keys in  
> an hash.

Wouldn’t a simpler solution be to use OpenStruct?

require ‘ostruct’

hash={“my\_attribute”=\>“value”}  
s=OpenStruct.new(hash)  
puts s.my\_attribute

> value  
> s.my\_attribute=“new”  
> =\> “new”  
> puts s.my\_attribute  
> new  
> =\> nil

And you get the freebie of creating new keys from the setter:

s.new\_attribute=“better”  
=\> “better”  
puts s.new\_attribute

> better

Cheers,  
V.-

---

<div class="post-metadata">

**Author:** ![andre83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bcef8e/32.png) [@andre83](https://www.ruby-forum.com/u/andre83)\
**Post date:** [July 23, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/generate-accessors-for-an-hash/143865/4 "2008-07-23T15:11:58Z")

</div>

James C. wrote:

> You might be better off with method\_missing
> 
> class Hash  
> def method\_missing(name, value = nil)  
> name = name.to\_s  
> if name =~ /=$/  
> self[name.sub(/=$/, ‘’)] = value  
> else  
> return self[name]  
> end  
> end  
> end

Thank you very much, that’s exactly what I did. Strange ruby behaviour  
anyway. 😛  
bye,

Andrea Gianarro
