Class: Filedict::Hash

Inherits:
Object
  • Object
show all
Defined in:
ext/filedictrb/hash.c

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Object

Ruby method definitions



74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/filedictrb/hash.c', line 74

static VALUE fd_hash_initialize(VALUE self, VALUE filename) {
    fd_hash_t *fd_hash = RTYPEDDATA_DATA(self);

    const char *filename_cstr = StringValuePtr(filename);
    filedict_open(&fd_hash->filedict, filename_cstr);

    if (fd_hash->filedict.error) {
        rb_raise(rb_eArgError, "Filedict error: %s", fd_hash->filedict.error);
    }

    return self;
}

Instance Method Details

#[](key) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/filedictrb/hash.c', line 109

static VALUE fd_hash_access(VALUE self, VALUE key) {
    fd_hash_t *fd_hash = RTYPEDDATA_DATA(self);

    VALUE result = rb_class_new_instance(0, NULL, cSet);
    const char *key_cstr = StringValuePtr(key);

    filedict_read_t read = filedict_get(&fd_hash->filedict, key_cstr);

    int success = 1;
    while (success && read.value) {
        rb_funcall(result, id_add, 1, rb_str_new_cstr(read.value));
        success = filedict_get_next(&read);
    }

    rb_ivar_set(result, id_fd_hash, self);
    rb_ivar_set(result, id_fd_key, key);

    return result;
}

#at(key) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/filedictrb/hash.c', line 109

static VALUE fd_hash_access(VALUE self, VALUE key) {
    fd_hash_t *fd_hash = RTYPEDDATA_DATA(self);

    VALUE result = rb_class_new_instance(0, NULL, cSet);
    const char *key_cstr = StringValuePtr(key);

    filedict_read_t read = filedict_get(&fd_hash->filedict, key_cstr);

    int success = 1;
    while (success && read.value) {
        rb_funcall(result, id_add, 1, rb_str_new_cstr(read.value));
        success = filedict_get_next(&read);
    }

    rb_ivar_set(result, id_fd_hash, self);
    rb_ivar_set(result, id_fd_key, key);

    return result;
}