Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail From: Brian Candler Newsgroups: comp.lang.ruby Subject: Initialize Struct from Hash Date: Thu, 28 Apr 2011 12:00:24 -0500 Organization: Service de news de lacave.net Lines: 30 Message-ID: <919600ef40c16b10cb0f01a757234376@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1304010200 49942 65.111.164.187 (28 Apr 2011 17:03:20 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Thu, 28 Apr 2011 17:03:20 +0000 (UTC) X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 382310 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <919600ef40c16b10cb0f01a757234376@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:3631 I just want to check I've not missed something here. Is there a built-in way to initialize a Struct from a hash of key/value pairs? That is, can I shorten the following? K1 = Struct.new :foo, :bar module FixStruct def set(h) h.each { |k,v| self[k] = v } self end end class K1 include FixStruct end k1 = K1.new.set(:bar=>456, :foo=>123) p k1 (I'm talking about real Struct here, not OpenStruct etc) Thanks, Brian. -- Posted via http://www.ruby-forum.com/.