Skip to content

Commit 13031ff

Browse files
authored
Merge pull request #155 from sue445/feature/NewGoStruct_doc
Add example doc to `NewGoStruct` and `GetGoStruct`
2 parents 84c6b60 + dd7f96f commit 13031ff

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

ruby/go_struct.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,74 @@ func goobj_free(obj unsafe.Pointer) {
2929

3030
// NewGoStruct create Ruby object from Go object
3131
//
32-
// See also [RbDefineAllocFunc]
32+
// Example
33+
//
34+
// /*
35+
// package main
36+
//
37+
// #include "example.h"
38+
//
39+
// VALUE go_struct_alloc(VALUE klass);
40+
// void rb_example_go_struct_set(VALUE self, VALUE x, VALUE y);
41+
// VALUE rb_example_go_struct_get(VALUE self);
42+
// */
43+
// import "C"
44+
//
45+
// import (
46+
// "github.com/sue445/go-gem-wrapper/ruby"
47+
// "unsafe"
48+
// )
49+
//
50+
// // GoStruct is internal reality of Ruby `Example::GoStruct`
51+
// type GoStruct struct {
52+
// x int
53+
// y int
54+
// }
55+
//
56+
// //export go_struct_alloc
57+
// func go_struct_alloc(klass C.VALUE) C.VALUE {
58+
// data := GoStruct{}
59+
// return C.VALUE(ruby.NewGoStruct(ruby.VALUE(klass), unsafe.Pointer(&data)))
60+
// }
61+
//
62+
// //export rb_example_go_struct_set
63+
// func rb_example_go_struct_set(self C.VALUE, x C.VALUE, y C.VALUE) {
64+
// data := (*GoStruct)(ruby.GetGoStruct(ruby.VALUE(self)))
65+
// data.x = ruby.NUM2INT(ruby.VALUE(x))
66+
// data.y = ruby.NUM2INT(ruby.VALUE(y))
67+
// }
68+
//
69+
// //export rb_example_go_struct_get
70+
// func rb_example_go_struct_get(self C.VALUE) C.VALUE {
71+
// data := (*GoStruct)(ruby.GetGoStruct(ruby.VALUE(self)))
72+
//
73+
// ret := []ruby.VALUE{
74+
// ruby.INT2NUM(data.x),
75+
// ruby.INT2NUM(data.y),
76+
// }
77+
//
78+
// return C.VALUE(ruby.Slice2rbAry(ret))
79+
// }
80+
//
81+
// //export Init_example
82+
// func Init_example() {
83+
// rb_mExample := ruby.RbDefineModule("Example")
84+
//
85+
// // Create Example::GoStruct class
86+
// rb_cGoStruct := ruby.RbDefineClassUnder(rb_mExample, "GoStruct", ruby.VALUE(C.rb_cObject))
87+
//
88+
// ruby.RbDefineAllocFunc(rb_cGoStruct, C.go_struct_alloc)
89+
//
90+
// ruby.RbDefineMethod(rb_cGoStruct, "set", C.rb_example_go_struct_set, 2)
91+
// ruby.RbDefineMethod(rb_cGoStruct, "get", C.rb_example_go_struct_get, 0)
92+
// }
3393
func NewGoStruct(klass VALUE, goobj unsafe.Pointer) VALUE {
3494
return VALUE(C.NewGoStruct(C.VALUE(klass), goobj))
3595
}
3696

3797
// GetGoStruct returns Go object from Ruby object
98+
//
99+
// See also [NewGoStruct]
38100
func GetGoStruct(obj VALUE) unsafe.Pointer {
39101
return C.GetGoStruct(C.VALUE(obj))
40102
}

0 commit comments

Comments
 (0)