# ActAsHash

**URL:** <https://www.ruby-forum.com/t/actashash/119074>\
**Category:** Ruby\
**Created:** [November 2, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/actashash/119074 "2007-11-02T21:36:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [November 2, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/actashash/119074/1 "2007-11-02T21:36:38Z")

</div>

I have no idea if someone hacked something like this already, i tried  
searching with google but it’s hard to type good keys … anyway here’s  
what i wrote for SOAP4R that had LOTS of instance variables that i  
already had in hash and wanted eay way to assign them.

this should work for almost every class and should provide nice and easy  
way to assigning values to instance variables in hash-like way.

of course it exposes all instance variables so encuspulaction (or  
whatever you spell it) goes flying out of window.

module ActAsHash  
def   
self.instance\_variable\_get(("@"+index.to\_s).to\_sym)  
end

def []=(index,value)  
self.instance\_variable\_set(("@"+index.to\_s).to\_sym, value)  
end

def from\_hash(hash)  
hash.each\_pair do |k,v|  
self[k]=v  
end  
end  
end

and use it simply like

class A  
include ActAsHash

attr\_accessor :foo  
end

a = A.new  
a[“foo”] = :bar  
a.foo =\> :bar  
a[:foo] =\> :bar

i hope it’s usefull for someone 🙂 mayby it can find place in Facets 😉

---

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [November 3, 2007, 12:42am UTC](https://www.ruby-forum.com/t/actashash/119074/2 "2007-11-03T00:42:46Z")

</div>

Marcin R. wrote:

> ```
> self.instance_variable_get(("@"+index.to_s).to_sym)
> 
> ```

ISTR that “@”+index.to\_s is slower than “@#{index}”.

Also, you don’t need to call #to\_sym. The string is enough.

---

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [November 3, 2007, 3:07am UTC](https://www.ruby-forum.com/t/actashash/119074/3 "2007-11-03T03:07:02Z")

</div>

Joel VanderWerf wrote:

> Marcin R. wrote:
> 
> > ```
> > self.instance_variable_get(("@"+index.to_s).to_sym)
> > 
> > ```
> 
> ISTR that “@”+index.to\_s is slower than “@#{index}”.  
> i think concantation is faster then creating new one and i have no idea  
> what ISTR mean
> 
> Also, you don’t need to call #to\_sym. The string is enough.

yep that’s true 🙂 i didn’t know that.

thanks for fixes

---

<div class="post-metadata">

**Author:** ![marcin\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@marcin\_r](https://www.ruby-forum.com/u/marcin_r)\
**Post date:** [November 3, 2007, 3:26am UTC](https://www.ruby-forum.com/t/actashash/119074/4 "2007-11-03T03:26:19Z")

</div>

Marcin R. wrote:

> Joel VanderWerf wrote:
> 
> > Marcin R. wrote:
> > 
> > > ```
> > > self.instance_variable_get(("@"+index.to_s).to_sym)
> > > 
> > > ```
> > 
> > ISTR that “@”+index.to\_s is slower than “@#{index}”.  
> > i think concantation is faster then creating new one and i have no idea  
> > what ISTR mean

Eh. It’s not as much as I remembered:

require ‘benchmark’

Benchmark.bmbm(12) do |bm|  
n = 1\_000\_000  
index = “foo”  
@foo = 123

bm.report(’"@"+index.to\_s’) do  
n.times do  
instance\_variable\_get("@"+index.to\_s)  
end  
end

bm.report(’"@#{index}"’) do  
n.times do  
instance\_variable\_get("@#{index}")  
end  
end  
end

**END**

Rehearsal --------------------------------------------------  
“@”+index.to\_s 1.170000 0.010000 1.180000 ( 1.178025)  
“@#{index}” 0.960000 0.000000 0.960000 ( 0.954115)  
----------------------------------------- total: 2.140000sec

```
                  user system total real

```

“@”+index.to\_s 1.170000 0.000000 1.170000 ( 1.174559)  
“@#{index}” 0.940000 0.000000 0.940000 ( 0.945398)

ISTR that ISTR means I seem to recall, but I might be wrong 😉
