Ruby/DL union! method syntax

G

gregarican

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?
 
T

Takaaki Tateishi

gregarican said:
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
 
G

gregarican

Takaaki said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,833
Latest member
BettyeMacf

Latest Threads

Top