### Home Classes Methods

### In Files

- lib/ffi/enum.rb

### Parent

Object

### Methods

- ::new
- #[]
- #find
- #from_native
- #native_type
- #symbol_map
- #symbols
- #to_h
- #to_hash
- #to_native

### Included Modules

- FFI::DataConverter

### Files

- Makefile
- ChangeLog
- LICENSE
- README
- compile
- configure
- depcomp
- stamp-vti
- install-sh
- libtool-version
- mdate-sh
- missing

### Class/Module Index

Quicksearch

- FFI
- FFI::AbstractMemory
- FFI::ArrayType
- FFI::AutoPointer
- FFI::AutoPointer::CallableReleaser
- FFI::AutoPointer::DefaultReleaser
- FFI::AutoPointer::Releaser
- FFI::Buffer
- FFI::ConstGenerator
- FFI::ConstGenerator::Constant
- FFI::DataConverter
- FFI::DynamicLibrary
- FFI::DynamicLibrary::Symbol
- FFI::Enum
- FFI::Enums
- FFI::Function
- FFI::FunctionType
- FFI::Generator
- FFI::Generator::Task
- FFI::IO
- FFI::Library
- FFI::ManagedStruct
- FFI::MemoryPointer
- FFI::NotFoundError
- FFI::NullPointerError
- FFI::Platform
- FFI::PlatformError
- FFI::Pointer
- FFI::StrPtrConverter
- FFI::Struct
- FFI::Struct::InlineArray
- FFI::Struct::ManagedStructConverter
- FFI::StructByReference
- FFI::StructByValue
- FFI::StructGenerator
- FFI::StructGenerator::Field
- FFI::StructLayout
- FFI::StructLayout::Array
- FFI::StructLayout::Enum
- FFI::StructLayout::Field
- FFI::StructLayout::Function
- FFI::StructLayout::InnerStruct
- FFI::StructLayout::Mapped
- FFI::StructLayout::Number
- FFI::StructLayout::Pointer
- FFI::StructLayout::String
- FFI::StructLayoutBuilder
- FFI::TypesGenerator
- FFI::Union
- FFI::VariadicInvoker

No matching classes.

# FFI::Enum

Represents a C enum.

For a C enum:

    enum fruits {
      apple,
      banana,
      orange,
      pineapple
    };

are defined this vocabulary:

- a symbol is a word from the enumeration (ie. apple, by example);

- a value is the value of a symbol in the enumeration (by example, apple has value 0 and banana 1).

### Attributes

tag[R]

### Public Class Methods

new(info, tag=nil) click to toggle source

@param [nil, Enumerable] info @param tag enum tag

    # File lib/ffi/enum.rb, line 80
    def initialize(info, tag=nil)
      @tag = tag
      @kv_map = Hash.new
      unless info.nil?
        last_cst = nil
        value = 0
        info.each do |i|
          case i
          when Symbol
            raise ArgumentError, "duplicate enum key" if @kv_map.has_key?(i)
            @kv_map[i] = value
            last_cst = i
            value += 1
          when Integer
            @kv_map[last_cst] = i
            value = i+1
          end
        end
      end
      @vk_map = @kv_map.invert
    end

### Public Instance Methods

[](query) click to toggle source

Get a symbol or a value from the enum. @overload [](query)

    Get enum value from symbol.
    @param [Symbol] query
    @return [Integer]

@overload [](query)

    Get enum symbol from value.
    @param [Integer] query
    @return [Symbol]

    # File lib/ffi/enum.rb, line 116
    def [](query)
      case query
      when Symbol
        @kv_map[query]
      when Integer
        @vk_map[query]
      end
    end

Also aliased as: find
find(query) click to toggle source
Alias for: []
from_native(val, ctx) click to toggle source

@param val @return symbol name if it exists for `val`.

    # File lib/ffi/enum.rb, line 156
    def from_native(val, ctx)
      @vk_map[val] || val
    end

native_type() click to toggle source

Get native type of Enum @return [Type::INT]

    # File lib/ffi/enum.rb, line 137
    def native_type
      Type::INT
    end

symbol_map() click to toggle source

Get the symbol map. @return [Hash]

    # File lib/ffi/enum.rb, line 128
    def symbol_map
      @kv_map
    end

Also aliased as: to_h, to_hash
symbols() click to toggle source

@return [Array] enum symbol names

    # File lib/ffi/enum.rb, line 103
    def symbols
      @kv_map.keys
    end

to_h() click to toggle source
Alias for: symbol_map
to_hash() click to toggle source
Alias for: symbol_map
to_native(val, ctx) click to toggle source

@param [Symbol, Integer, to_int] val @param ctx unused @return [Integer] value of a enum symbol

    # File lib/ffi/enum.rb, line 144
    def to_native(val, ctx)
      @kv_map[val] || if val.is_a?(Integer)
        val
      elsif val.respond_to?(:to_int)
        val.to_int
      else
        raise ArgumentError, "invalid enum value, #{val.inspect}"
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.