forked from mitchellh/go-mruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
48 lines (40 loc) · 1.29 KB
/
hash.go
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
package mruby
// #include "gomruby.h"
import "C"
// Hash represents an MrbValue that is a Hash in Ruby.
//
// A Hash can be obtained by calling the Hash function on MrbValue.
type Hash struct {
*MrbValue
}
// Delete deletes a key from the hash, returning its existing value,
// or nil if there wasn't a value.
func (h *Hash) Delete(key Value) (*MrbValue, error) {
keyVal := key.MrbValue(&Mrb{h.state}).value
result := C.mrb_hash_delete_key(h.state, h.value, keyVal)
val := newValue(h.state, result)
if val.Type() == TypeNil {
val = nil
}
return val, nil
}
// Get reads a value from the hash.
func (h *Hash) Get(key Value) (*MrbValue, error) {
keyVal := key.MrbValue(&Mrb{h.state}).value
result := C.mrb_hash_get(h.state, h.value, keyVal)
return newValue(h.state, result), nil
}
// Set sets a value on the hash
func (h *Hash) Set(key, val Value) error {
keyVal := key.MrbValue(&Mrb{h.state}).value
valVal := val.MrbValue(&Mrb{h.state}).value
C.mrb_hash_set(h.state, h.value, keyVal, valVal)
return nil
}
// Keys returns the array of keys that the Hash has. This is returned
// as an *MrbValue since this is a Ruby array. You can iterate over it as
// you see fit.
func (h *Hash) Keys() (*MrbValue, error) {
result := C.mrb_hash_keys(h.state, h.value)
return newValue(h.state, result), nil
}