1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
##Synopsis
CStruct is a simulation of the C language's struct.Its main purpose is to manipulate binary-data conveniently in Ruby. It can be used in:
* Binary file IO like C.
* The parameter of the OS's API.(e.g. Win32)
* Other...
Lean more: [http://cstruct.rubyforge.org/][1]
##Getting Started
###Install CStruct
Install CStruct is easy.
gem install cstruct
###Using CStruct
Let's see an example,struct Point in C language(32-bit platform) like this:
```C
struct Point
{
int x;
int y;
};
```
How to represent struct Point in Ruby? You can use CStruct to do it.
```ruby
class Point < CStruct
int32:x
int32:y
end
```
Example:
```ruby
require 'cstruct'
# struct Point in Ruby:
class Point < CStruct
int32:x
int32:y
end
# create a Point's instance
point = Point.new
# assign like as C language
point.x = 10
point.y = 20
puts "point.x = #{point.x},point.y = #{point.y}"
```
###Using Win32Struct
like this:
```C
typedef struct _OSVERSIONINFOEXA {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
CHAR szCSDVersion[ 128 ];
WORD wServicePackMajor;
WORD wReserved[2];
} OSVERSIONINFOEXA;
```
in Ruby:
```Ruby
class OSVERSIONINFOEXA < Win32Struct
DWORD :dwOSVersionInfoSize
DWORD :dwMajorVersion
DWORD :dwMinorVersion
DWORD :dwBuildNumber
DWORD :dwPlatformId
CHAR :szCSDVersion,[ 128 ]
WORD :wServicePackMajor
WORD :wServicePackMinor
WORD :wReserved,[2]
end
```
##Lean More
Please see also [Documents][2] and [Examples][3].
[1]:http://cstruct.rubyforge.org/
[2]:http://cstruct.rubyforge.org/documents.html
[3]:http://cstruct.rubyforge.org/examples.html
|