[Ruby] unless block on one line
Laurel Fan
laurel.fan at gmail.com
Fri Jan 26 12:16:05 PST 2007
On 1/26/07, ben wiseley <wiseleyb at gmail.com> wrote:
> On this topic ... is there a better way to do things like this where Contact
> is just a straight class (doesn't inherit from anything like Model)
>
> @contact = Contact.new
> @contact.name = params[:contact][:name]
> @contact.email = params[:contact][:email]
> @contact.subject = params[:contact][:subject]
> @contact.message = params[:contact][:message]
>
> Anything like "update_attributes" for normal classes?
OpenStruct has a new method that takes a hash:
http://www.ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html
so if you're willing to deal with the looseness of openstruct you can
do that. (activerecord attributes are essentially a hash too).
It shouldn't be too hard to implement yourself either:
def update_attributes(hash)
hash.each |name, value| do
send ("#{name}=", value)
end
end
I'm not sure if there's a more elegant way. Sometimes it seems like
Ruby likes to hide things like this.
--
Laurel Fan
http://blog.gorgorg.org
More information about the Ruby
mailing list