Ruby/DL union! method syntax

I read the Ruby/DL docs on using the struct! method and have an
understanding about it. How about the union! method? I have googled
around and can't find any examples of its use. The following test code
doesn't seem to work:

···

--------------------------
require 'dl'
thisStruct=DL.malloc(4)
thisStruct.struct!('L', :longPointer)
thatStruct=DL.malloc(2)
thatStruct.struct!('H', :shortPointer)
theUnion=DL.malloc(6)
theUnion.union!(thisStruct, thatStruct)

Reading the RDoc entries for the union! method it appears to work the
same as the struct! method. First pass along the data types, next pass
along the element names. But I thought a union just connects existing
structs. How does that work with the first parameter of data types?

Does anyone out there have some example they can provide that will
accomplish what my test code is trying to do?

gregarican wrote:

I read the Ruby/DL docs on using the struct! method and have an
understanding about it. How about the union! method? I have googled
around and can't find any examples of its use. The following test code
doesn't seem to work:
--------------------------
require 'dl'
thisStruct=DL.malloc(4)
thisStruct.struct!('L', :longPointer)
thatStruct=DL.malloc(2)
thatStruct.struct!('H', :shortPointer)
theUnion=DL.malloc(6)
theUnion.union!(thisStruct, thatStruct)

You should pass a signature and labels like struct!.

theUnion.union!('LH', :long, :short)
theUnion[:long] = 2**16
p theUnion[:long] # => 65536
p theUnion[:short] # => 0

···

--
Takaaki Tateishi <ttate@ttsky.net>

Takaaki Tateishi wrote:

You should pass a signature and labels like struct!.

theUnion.union!('LH', :long, :short)
theUnion[:long] = 2**16
p theUnion[:long] # => 65536
p theUnion[:short] # => 0

Oh I see. Thanks for the reply. I thought that a union was defined as
multiple structs brought together in a modular fashion. So I was trying
to invoke the union! method with struct! members as parameters. I will
try it the way you have outlined it. Thanks again!

Thanks