gomruby

package module
v0.0.0-...-c061f39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2014 License: BSD-2-Clause Imports: 5 Imported by: 0

README

GoMRubyBuild Status

Package gomruby embeds mruby (mini Ruby) VM into Go.

Documentation.

Installation

It's slightly more than just go get:

go get -d github.com/AlekSi/gomruby
cd $GOPATH/src/github.com/AlekSi/gomruby
make

mruby is built statically, use gomruby as typical Go package.

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) Delete

func (c *LoadContext) Delete()

Deletes 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 New

func New() *MRuby

Creates new mruby VM. Panics if it's not possible.

func (*MRuby) Delete

func (m *MRuby) Delete()

Deletes 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.

type Symbol

type Symbol string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL