Maintenance of Ruby 2.0.0 ended on February 24, 2016. Read more

In Files

  • fiddle/lib/fiddle/struct.rb


A C union wrapper

Public Class Methods

malloc(types, func=nil) click to toggle source

Allocates a C union the types provided.

When the instance is garbage collected, the C function func is called.

               # File fiddle/lib/fiddle/struct.rb, line 216
def CUnionEntity.malloc(types, func=nil)
  addr = Fiddle.malloc(CUnionEntity.size(types)), types, func)
size(types) click to toggle source

Returns the size needed for the union with the given types.

  [ Fiddle::TYPE_DOUBLE,
    Fiddle::TYPE_VOIDP ]) #=> 8
               # File fiddle/lib/fiddle/struct.rb, line 228
def CUnionEntity.size(types) { |type, count = 1|
    PackInfo::SIZE_MAP[type] * count

Public Instance Methods

set_ctypes(types) click to toggle source

Calculate the necessary offset and for each union member with the given types

               # File fiddle/lib/fiddle/struct.rb, line 236
def set_ctypes(types)
  @ctypes = types
  @offset =, 0)
  @size   = self.class.size types

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit