Class: Raindrops::Struct

### Home Classes Methods

### In Files

- lib/raindrops/struct.rb

### Parent

Object

### Methods

- ::new

### Files

- LICENSE
- NEWS
- README

### Class/Module Index

Quicksearch

- Raindrops
- Raindrops::Aggregate
- Raindrops::Aggregate::LastDataRecv
- Raindrops::Aggregate::PMQ
- Raindrops::InetDiagSocket
- Raindrops::LastDataRecv
- Raindrops::Linux
- Raindrops::ListenStats
- Raindrops::Middleware
- Raindrops::Middleware::Proxy
- Raindrops::Middleware::Stats
- Raindrops::Struct
- Raindrops::TCP_Info
- Raindrops::Watcher

No matching classes.

# Raindrops::Struct

This is a wrapper around Raindrops objects much like the core Ruby Struct can be seen as a wrapper around the core Array class. It's usage is similar to the core Struct class, except its fields may only be used to house unsigned long integers.

    class Foo < Raindrops::Struct.new(:readers, :writers)
    end

    foo = Foo.new 0, 0

    foo.incr_writers    -> 1
    foo.incr_readers    -> 1

### Public Class Methods

new(*members) click to toggle source

returns a new class derived from Raindrops::Struct and supporting the given `members` as fields, just like Struct.new in core Ruby.

    # File lib/raindrops/struct.rb, line 19
    def self.new(*members)
      members = members.map { |x| x.to_sym }.freeze
      str = def initialize(*values)  (MEMBERS.size >= values.size) or raise ArgumentError, "too many arguments"  @raindrops = Raindrops.new(MEMBERS.size)  values.each_with_index { |val,i| @raindrops[i] = values[i] }enddef initialize_copy(src)  @raindrops = src.instance_variable_get(:@raindrops).dupenddef []=(index, value)  @raindrops[index] = valueenddef [](index)  @raindrops[index]enddef to_hash  ary = @raindrops.to_ary  rv = {}  MEMBERS.each_with_index { |member, i| rv[member] = ary[i] }  rvend

      members.each_with_index do |member, i|
        str << "def incr_#{member}; @raindrops.incr(#{i}); end; "               "def decr_#{member}; @raindrops.decr(#{i}); end; "               "def #{member}; @raindrops[#{i}]; end; "               "def #{member}=(val); @raindrops[#{i}] = val; end; "
      end

      klass = Class.new
      klass.const_set(:MEMBERS, members)
      klass.class_eval(str)
      klass
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.