# How about using hashes as helpers?

**URL:** <https://www.ruby-forum.com/t/how-about-using-hashes-as-helpers/169941>\
**Category:** Rails\
**Created:** [June 10, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/how-about-using-hashes-as-helpers/169941 "2009-06-10T14:46:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![night\_stalker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f0a364/32.png) [@night\_stalker](https://www.ruby-forum.com/u/night_stalker)\
**Post date:** [June 10, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/how-about-using-hashes-as-helpers/169941/1 "2009-06-10T14:46:20Z")

</div>

i had a simple idea.

first, think about:  
p = proc{|x| x\*2}

we can call the proc with ‘[]’:  
p[3] # =\> 6

there’s another way, which caches the result:  
p = Hash.new{|h, x| h[x] = x\*2}

we have the same interface as a proc call:  
p[3] # =\> 6

then, a little bit more:  
class Proc  
def memoise  
Hash.new {|h,k| h[k] = self[k]}  
end  
end

finally defining a cached ‘proc’ is clearer:  
p = proc{|x| x\*2}.memoise

So, with the benifits of the above things, how about switching rails  
helper methods from defs to procs ? then you can define cached or  
uncached helpers easily, and you can even use a hash or anything (which  
responds to []) as a helper.

maybe route helpers can be a little bit faster ?
