\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt>class<\/span> Hashit<\/span>\n<\/tt> def<\/span> initialize<\/span>(hash)\n<\/tt> hash.each do<\/span> |k,v|\n<\/tt> self<\/span>.instance_variable_set("<\/span>@<\/span>#{<\/span>k}<\/span><\/span>"<\/span><\/span>, v) ## create and initialize an instance variable for this key\/value pair<\/span>\n<\/tt> self<\/span>.class.send(:define_method<\/span>, k, proc{self<\/span>.instance_variable_get("<\/span>@<\/span>#{<\/span>k}<\/span><\/span>"<\/span><\/span>)}) ## create the getter that returns the instance variable<\/span>\n<\/tt> self<\/span>.class.send(:define_method<\/span>, "<\/span>#{<\/span>k}<\/span><\/span>=<\/span>"<\/span><\/span>, proc{|v| self<\/span>.instance_variable_set("<\/span>@<\/span>#{<\/span>k}<\/span><\/span>"<\/span><\/span>, v)}) ## create the setter that sets the instance variable<\/span>\n<\/tt> end<\/span>\n<\/tt> end<\/span>\n<\/tt>end<\/span>\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\nNotice the self.class.send(:define_method ...) rather than self.define_method, this is a hack to overcome the fact that define_method() is private. I had come across this when trying to figure out the post mentioned above. Found the information to solve this here<\/a>. \nOk, so on to the Controller that creates the Hashit object:<\/p>\n \n\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt>class<\/span> TestItController<\/span> < ApplicationController<\/span>\n<\/tt> def<\/span> index<\/span>\n<\/tt> hashit = {:support_email<\/span> => "<\/span>test@test.com<\/span>"<\/span><\/span>,\n<\/tt> :allow_comments<\/span> => 0<\/span>}\n<\/tt> @hashit<\/span> = Hashit<\/span>.new(hashit)\n<\/tt> end<\/span>\n<\/tt>end<\/span>\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\nWell that is easy, pass in a hash and get an object. Here is what @hashit looks like at this point:<\/p>\n \n\n\n1\n<\/tt>2\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt>#<Hashit:0xb6a65110 @allow_comments=0, @support_email="test@test.com"><\/span>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\nAnd of course, now the view, what we wanted to clean up and make more elegant. Here is what the user started with: \nNote: In this example @hashit is an actual hash, not a class object.<\/p>\n \n\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt><% form_tag :action<\/span> => '<\/span>config<\/span>'<\/span><\/span>, :method<\/span> => :post<\/span> do<\/span> %><\/span>\n<\/tt> <%= text_field 'settings', 'support_email', :size =<\/span>><\/span><\/span> 20<\/span>, :value<\/span> => @hashit<\/span>['<\/span>support_email<\/span>'<\/span><\/span>]%>\n<\/tt> ...\n<\/tt><% end<\/span> %><\/span>\n<\/tt>\n<\/tt><\/span><\/span><\/pre>\n<\/td>\n<\/tr>\n<\/table>\nAnd here is what our view code looks like now:<\/p>\n \n\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt><% form_for :hashit<\/span>, :url<\/span> => {:action<\/span> => '<\/span>index<\/span>'<\/span><\/span>} do<\/span> |f| %><\/span>\n<\/tt> <%= f.text_field :support_email %<\/span>><\/span><\/span>\n<\/tt><% end<\/span> %><\/span>\n<\/tt>\n<\/tt><\/span><\/span><\/pre>\n<\/td>\n<\/tr>\n<\/table>\nMuch simpler, DRYer \ud83d\ude42 \nWell that is pretty much it, I suppose the next step would be to have a save method that updates the hash? This way we can do @hashit.save() and it will return a new hash that you can use. Well actually, that probably isn't too hard, lets see if I can do it real quick, class object back to hash ... \nWell, I am back and I was able to figure it out, here is the new class:<\/p>\n \n\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt>class<\/span> Hashit<\/span>\n<\/tt> def<\/span> initialize<\/span>(hash)\n<\/tt> hash.each do<\/span> |k,v|\n<\/tt> self<\/span>.instance_variable_set("<\/span>@<\/span>#{<\/span>k}<\/span><\/span>"<\/span><\/span>, v)\n<\/tt> self<\/span>.class.send(:define_method<\/span>, k, proc{self<\/span>.instance_variable_get("<\/span>@<\/span>#{<\/span>k}<\/span><\/span>"<\/span><\/span>)})\n<\/tt> self<\/span>.class.send(:define_method<\/span>, "<\/span> | | | | | | | | | | | |