Documentation ¶
Overview ¶
Package gomruby embeds mruby (mini Ruby) VM into Go.
Type conversions:
- nil, true, false, string - as expected
- Float <-> FIXME tricky
- Fixnum <-> FIXME tricky
- Symbol <-> gomruby.Symbol
- Array <-> []interface{}
- Hash <-> map[interface{}]interface{}
Example ¶
// create new VM instance and load context mruby := gomruby.New() defer mruby.Delete() context := mruby.NewLoadContext("select.rb") defer context.Delete() // this is user-supplied code userCode := ` message.include? "500" ` // define method code := fmt.Sprintf(` def select(message) %s end`, userCode) _, err := context.Load(code) if err != nil { panic(err) } // iterate over messages and select interesting for _, message := range []string{ `1.2.3.1 - - [04/Jun/2013:18:02:01 +0000] host "GET /foo HTTP/1.0" 200`, `1.2.3.2 - - [04/Jun/2013:18:02:02 +0000] host "GET /bar HTTP/1.0" 300`, `1.2.3.3 - - [04/Jun/2013:18:02:03 +0000] host "GET /baz HTTP/1.0" 400`, `1.2.3.4 - - [04/Jun/2013:18:02:04 +0000] host "GET /bzr HTTP/1.0" 500`, } { code := fmt.Sprintf("select(%q)", message) res, err := context.Load(code) if err != nil { panic(err) } fmt.Println(res.(bool)) }
Output: false false false true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LoadContext ¶
type LoadContext struct {
// contains filtered or unexported fields
}
mruby VM load context.
func (*LoadContext) Load ¶
func (c *LoadContext) Load(code string, args ...interface{}) (res interface{}, err error)
Loads mruby code. Arguments are exposed as ARGV array.
type MRuby ¶
type MRuby struct {
// contains filtered or unexported fields
}
mruby VM.
func (*MRuby) NewLoadContext ¶
func (m *MRuby) NewLoadContext(filename string) (context *LoadContext)
Creates new load context. Panics if it's not possible. Filename is used in error messages.
Click to show internal directories.
Click to hide internal directories.