Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3636
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Initialize Struct from Hash |
| Date | 2011-04-28 12:16 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <061feef934e8496634c868f2a00a4ff2@ruby-forum.com> (permalink) |
| References | <919600ef40c16b10cb0f01a757234376@ruby-forum.com> <BANLkTinnJvTxPXORtnne78=eZAmka5-qHQ@mail.gmail.com> |
"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#995567:
> On Thu, Apr 28, 2011 at 7:00 PM, Brian Candler <b.candler@pobox.com>
> 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]
> => #<struct K id=3, timestamp=8>
>
> Jesus.
Is there a hash anywhere in your code? How about this:
K1 = Struct.new :foo, :bar
class MyClass < K1
def initialize(hash)
super(*hash.values_at(:foo, :bar) )
end
end
puts MyClass.new(bar: 456, foo: 123)
(stolen from:
http://stackoverflow.com/questions/2680523/dry-ruby-initialization-with-hash-argument)
--
Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Initialize Struct from Hash Brian Candler <b.candler@pobox.com> - 2011-04-28 12:00 -0500
Re: Initialize Struct from Hash Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-28 12:05 -0500
Re: Initialize Struct from Hash Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-28 12:10 -0500
Re: Initialize Struct from Hash 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-28 12:16 -0500
Re: Initialize Struct from Hash Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-28 12:19 -0500
Re: Initialize Struct from Hash Brian Candler <b.candler@pobox.com> - 2011-04-28 15:24 -0500
Re: Initialize Struct from Hash Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-04-28 15:50 -0500
Re: Initialize Struct from Hash Robert Klemme <shortcutter@googlemail.com> - 2011-04-29 03:06 -0500
Re: Initialize Struct from Hash Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-04-29 10:27 -0500
Re: Initialize Struct from Hash Brian Candler <b.candler@pobox.com> - 2011-04-29 03:22 -0500
csiph-web