Bob Showalter
2007-Nov-28 19:12 UTC
OT: Re: [Rails] OpenHash class I thought I would share...
On Nov 28, 2007 10:43 AM, uncle <aktxyz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I love OpenStruct, but it has no ability to iterate over the members. > I had extended it, but recently I use this little class. Anyone see > any issues with it ? > > class OpenHash < Hash > def method_missing(name, value=nil) > key = name.to_s.sub(/[=?!]$/,'''').to_sym > self[key] = value if name.to_s[-1,1] == "=" > return self[key] > end > end(This is off-topic for the rails list). IMO, subclassing Hash like this isn''t a good idea. Hash has too many methods to get intermixed with your "open" methods. Why not just expose the internal hash that OpenStruct uses: class OpenStruct attr_reader :table end Then you can iterate over myobj.table --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---