Zouplaz
September 15, 2006, 12:21pm
1
Hello, I wonder if it’s possible to merge two #struct objects ?
For example merging this one
#<struct #Class:0xb7986c00 ident=“237006/19748895”, details=“Some text
here”>
With this one
#<struct #Class:0xb79893b0 libelle=“Nice object”, superficie=0,
localisation=“This town”, prix=“1000”>
How can I do ??
Thanks
Zouplaz
September 15, 2006, 1:01pm
2
Zouplaz wrote:
Hello, I wonder if it’s possible to merge two #struct objects ?
It’s possible, but it’s probably better to use Hashes instead. You can
always convert a Hash to a Struct [1] after you’re done fiddling with
it. But if you really want to merge two structs, you could do something
like this:
def struct_join(main, other)
mems = main.members + other.members
vals = main.values + other.values
Struct.new(nil, *mems).new(*vals)
end
[1] http://rubyforge.org/snippet/detail.php?type=snippet&id=1
Regards,
Jordan
Zouplaz
September 15, 2006, 1:05pm
3
le 15/09/2006 12:55, MonkeeSage nous a dit:
vals = main.values + other.values
Struct.new(nil, *mems).new(*vals)
end
[1] http://rubyforge.org/snippet/detail.php?type=snippet&id=1
Regards,
Jordan
Thank you so much !! I spent 3 hours on this…
Zouplaz
September 15, 2006, 11:53pm
4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
MonkeeSage wrote:
def struct_join(main, other)
mems = main.members + other.members
vals = main.values + other.values
Struct.new(nil, *mems).new(*vals)
end
I like your variable naming style here. I’d normally write ‘members’
& ‘values’, but yours are concise & unambiguous.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
iD8DBQFFCyC1mV9O7RYnKMcRAk72AJ4zHVxoQimXdEZQWV4sT2wXGZcy0QCgqvsP
KVI2IzLsGLHWN6EQ3B0wSrA=
=28Ta
-----END PGP SIGNATURE-----