dtrusty-U6r7FCv0h4VBDgjK7y7TUQ@public.gmane.org
2007-Mar-30 18:49 UTC
Is there a way to store items in a hash, yet refer to them as class methods?
Hi, Many of the view helpers require an object with accessor methods as parameters. 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: person[''description''] 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- person[''description''] interchangeably. 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 already? Thanks, David --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bill Walton
2007-Mar-30 19:38 UTC
Re: Is there a way to store items in a hash, yet refer to them as class methods?
Hi David, dtrusty-U6r7FCv0h4VBDgjK7y7TUQ@public.gmane.org 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, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
John Barnette
2007-Mar-30 20:24 UTC
Re: Is there a way to store items in a hash, yet refer to them as class methods?
This is dead simple and read-only, but how ''bout something like:
class HashyTest < Test::Unit::TestCase
def test_hashy_things
hash = HashWithIndifferentAccess.new
hash["monkeys"] = "These are monkeys."
hash["monkeys!"] = "Yes, monkeys!"
hash[:"monkeys?"] = "Do you like monkeys too?"
def hash.method_missing(sym, *args)
self[sym]
end
assert_equal(hash["monkeys"], hash.monkeys)
assert_equal(hash["monkeys!"], hash.monkeys!)
assert_equal(hash[:"monkeys?"], hash.monkeys?)
end
end
~ j.
On 3/30/07, Bill Walton
<bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org>
wrote:>
> Hi David,
>
> dtrusty-U6r7FCv0h4VBDgjK7y7TUQ@public.gmane.org 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,
> Bill
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
John Barnette
2007-Mar-30 20:39 UTC
Re: Is there a way to store items in a hash, yet refer to them as class methods?
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]
super
end
~ j.
On 3/30/07, John Barnette
<jbarnette-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> This is dead simple and read-only, but how ''bout something like:
>
> class HashyTest < Test::Unit::TestCase
> def test_hashy_things
> hash = HashWithIndifferentAccess.new
>
> hash["monkeys"] = "These are monkeys."
> hash["monkeys!"] = "Yes, monkeys!"
> hash[:"monkeys?"] = "Do you like monkeys too?"
>
> def hash.method_missing(sym, *args)
> self[sym]
> end
>
> assert_equal(hash["monkeys"], hash.monkeys)
> assert_equal(hash["monkeys!"], hash.monkeys!)
> assert_equal(hash[:"monkeys?"], hash.monkeys?)
> end
> end
>
>
> ~ j.
>
> On 3/30/07, Bill Walton
<bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:
> >
> > Hi David,
> >
> > dtrusty-U6r7FCv0h4VBDgjK7y7TUQ@public.gmane.org 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,
> > Bill
> >
> >
> > > >
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Phlip
2007-Mar-31 04:02 UTC
Re: Is there a way to store items in a hash, yet refer to them as class methods?
John Barnette wrote:> def hash.method_missing(sym, *args) > self[sym] > endHere''s a mildly destructive "Monkey Patch", showing that technique to avoid 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('' '')}>", caller end return attributes[attr_name] end end Problem: It interferes with other Monkey Patches on REXML. (Too many monkeys 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: http://phlip.svnrepository.com/svn/yar_wiki http://phlip.svnrepository.com/svn/yar_wiki/lib/assert_xpath.rb -- Phlip http://flea.sourceforge.net/PiglegToo_1.html --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---