Ruby/DL union! method syntax

Discussion in 'Ruby' started by gregarican, Oct 15, 2005.

  1. gregarican

    gregarican Guest

    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, Oct 15, 2005
    #1
    1. Advertisements

  2. 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, Oct 17, 2005
    #2
    1. Advertisements

  3. gregarican

    gregarican Guest

    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
     
    gregarican, Oct 18, 2005
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.