From: "Jesús Gabriel y Galán" Date: 2011-04-29T02:19:19+09:00 Subject: Re: Initialize Struct from Hash On Thu, Apr 28, 2011 at 7:16 PM, 7stud -- wrote: > "Jesús Gabriel y Galán" wrote in post > #995567: >> On Thu, Apr 28, 2011 at 7:00 PM, Brian Candler >> wrote: >>> self >>> (I'm talking about real Struct here, not OpenStruct etc) >> ruby-1.8.7-p334 :001 > K = Struct.new :id,:timestamp >> => K >> ruby-1.8.7-p334 :002 > K[3,8] >> => # >> >> Jesus. > > Is there a hash anywhere in your code? How about this: Yep, I know. How about this? ruby-1.8.7-p334 :014 > h = {:id => 3, :timestamp => 6} => {:timestamp=>6, :id=>3} ruby-1.8.7-p334 :043 > K = Struct.new :id, :timestamp do ruby-1.8.7-p334 :044 > def self.from_hash h ruby-1.8.7-p334 :045?> self[*h.values_at(*K.members.map {|m| m.to_sym})] ruby-1.8.7-p334 :046?> end ruby-1.8.7-p334 :047?> end => K ruby-1.8.7-p334 :048 > K.from_hash h => # Jesus.