Forum: Ruby on Rails Is there a way to store items in a hash, yet refer to them a

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
32731bc4e4f02966dd6551eb8935180f?d=identicon&s=25 (Guest)
on 2007-03-30 20:50
(Received via mailing list)

Many of the view helpers require an object with accessor methods as

There are times however, when I want to store a collection of
variables in a hash,
and yet be able to use these helpers.

Look at this example:
<%= text_area "person", "description" %>
This works great for the case where I have a "person" object, with a
"description" attribute.
But what if I need to store the data in a hash, like this:
I don't want to write a wrapper function to get all the hash values.
I would like to
use a gem or other builtin mechanism to be able to refer to
person.description -or-

Is there a way to store variables in a collection (like a hash), and
use the keys of the
hash as accessor names in view helpers?  Has anyone written one


2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-03-30 21:39
(Received via mailing list)
Hi David, wrote:

> Is there a way to store variables in a collection
> (like a hash), and use the keys of the hash as
> accessor names in view helpers?

Cool idea.

Best regards,
C237cf537a06b60921c97804679e3b15?d=identicon&s=25 John Barnette (Guest)
on 2007-03-30 22:24
(Received via mailing list)
This is dead simple and read-only, but how 'bout something like:

class HashyTest < Test::Unit::TestCase
  def test_hashy_things
    hash =

    hash["monkeys"] = "These are monkeys."
    hash["monkeys!"] = "Yes, monkeys!"
    hash[:"monkeys?"] = "Do you like monkeys too?"

    def hash.method_missing(sym, *args)

    assert_equal(hash["monkeys"], hash.monkeys)
    assert_equal(hash["monkeys!"], hash.monkeys!)
    assert_equal(hash[:"monkeys?"], hash.monkeys?)

~ j.
C237cf537a06b60921c97804679e3b15?d=identicon&s=25 John Barnette (Guest)
on 2007-03-30 22:39
(Received via mailing list)
Hm. Might want to make this just slightly less confusing for methods
that don't map to a key:

    def hash.method_missing(sym, *args)
      include? sym and return self[sym]

~ j.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-03-31 06:03
(Received via mailing list)
John Barnette wrote:

>    def hash.method_missing(sym, *args)
>      self[sym]
>    end

Here's a mildly destructive "Monkey Patch", showing that technique to
writing too much attributes['blah'] all over the place.

class REXML::Element
  def method_missing(symbol)
    attr_name = symbol.id2name
    attr_name.gsub!(/!$/, '')

    unless attributes.has_key?(attr_name)
      raise NoMethodError,
            "missing attribute: `#{attr_name}` in "+
              "<#{name} #{attributes.keys.join(' ')}>",
    return attributes[attr_name]

Problem: It interferes with other Monkey Patches on REXML. (Too many
climbing that inheritance tree!) I need to get around to seeing if blah?
would work better, for a clean read, and if the patch could call some
equivalent of 'super' would allow other monkey patches to pass thru.

Here's its home project, a plugin, and its file:

This topic is locked and can not be replied to.