Class: StrHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/hwia_rails.rb,
ext/hwia/hwia.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



400
401
402
403
404
405
406
407
408
409
# File 'ext/hwia/hwia.c', line 400

static VALUE
rb_strhash_initialize(int argc, VALUE *argv, VALUE hash){
	VALUE constructor;
	rb_scan_args(argc, argv, "01", &constructor);
	if(TYPE(constructor) == T_HASH){
		return rb_strhash_update(hash,constructor);
	}else{
		return rb_call_super(argc,argv);
	}		
}

Class Method Details

.[](*args) ⇒ Object

hash.c



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'ext/hwia/hwia.c', line 314

static VALUE
rb_strhash_s_create(int argc, VALUE *argv, VALUE klass)
{
    VALUE hash;
    int i;

    if (argc == 1 && TYPE(argv[0]) == T_HASH) {
	hash = strhash_alloc0(klass);
	HASH_TBL(hash) = st_copy(HASH_TBL(argv[0]));
	HASH_TBL(hash)->type = &objstrhash;
	RHASH(hash)->ifnone = RHASH(argv[0])->ifnone;
	return rb_strhash_rehash(hash);
    }

    if (argc % 2 != 0) {
	rb_raise(rb_eArgError, "odd number of arguments for Hash");
    }

    hash = strhash_alloc(klass);
    for (i=0; i<argc; i+=2) {
        rb_strhash_aset(hash, argv[i], argv[i + 1]);
    }

    return hash;
}

Instance Method Details

#[]=(key, val) ⇒ Object



306
307
308
309
310
311
# File 'ext/hwia/hwia.c', line 306

static VALUE
rb_strhash_aset(VALUE hash, VALUE key, VALUE val){
	VALUE converted = rb_strhash_convert(hash,val);
	rb_hash_aset(hash, key, converted);
    return converted;
}

#convert(val) ⇒ Object

temp. public API



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'ext/hwia/hwia.c', line 285

static VALUE
rb_strhash_convert(VALUE hash, VALUE val)
{
    int i;
    VALUE values;

    switch (TYPE(val)) {
      case T_HASH:
           return rb_hash_strhash(val);    
      case T_ARRAY:
            values = rb_ary_new2(RARRAY_LEN(val));
            for (i = 0; i < RARRAY_LEN(val); i++) {
               VALUE el = RARRAY_PTR(val)[i];
               rb_ary_push(values, (TYPE(el) == T_HASH) ? rb_hash_strhash(el) : el);
            } 
           return values;
      default:
           return val;
    }
}

#dupObject

revist, same API, but may be clobbered



346
347
348
349
350
351
352
# File 'ext/hwia/hwia.c', line 346

static VALUE
rb_hash_strhash(VALUE hash)
{
	VALUE args[1];
	args[0] = hash;
	return rb_strhash_s_create(1, (VALUE *)args, rb_cStrHash);
}

#merge(hash2) ⇒ Object



411
412
413
414
415
416
# File 'ext/hwia/hwia.c', line 411

static VALUE
rb_strhash_merge(VALUE hash1, VALUE hash2){
	/* see note in Init */
	VALUE duped = rb_hash_strhash(hash1);
	return rb_strhash_update(hash1,hash2);
}

#merge!(hash2) ⇒ Object

hash.c



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'ext/hwia/hwia.c', line 384

static VALUE
rb_strhash_update(VALUE hash1, VALUE hash2)
{
#ifdef RUBY19	
	rb_hash_modify(hash1);
#endif
    hash2 = to_strhash(hash2);
    if (rb_block_given_p()) {
	rb_hash_foreach(hash2, rb_strhash_update_block_i, hash1);
    }
    else {
	rb_hash_foreach(hash2, rb_strhash_update_i, hash1);
    }
    return hash1;
}

#rehashObject

hash.c



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'ext/hwia/hwia.c', line 263

static VALUE
rb_strhash_rehash(VALUE hash)
{
    st_table *tbl;
#ifdef RUBY19
    if (RHASH(hash)->iter_lev > 0) {
	rb_raise(rb_eRuntimeError, "rehash during iteration");
    }
    rb_hash_modify_check(hash);
    if (!RHASH(hash)->ntbl)
        return hash;
#endif
    rb_hash_modify(hash);
    tbl = st_init_table_with_size(&objstrhash, HASH_TBL(hash)->num_entries);
    rb_hash_foreach(hash, rb_hash_rehash_i, (st_data_t)tbl);
    st_free_table(HASH_TBL(hash));
    HASH_TBL(hash) = tbl;

    return hash;
}

#store(key, val) ⇒ Object



306
307
308
309
310
311
# File 'ext/hwia/hwia.c', line 306

static VALUE
rb_strhash_aset(VALUE hash, VALUE key, VALUE val){
	VALUE converted = rb_strhash_convert(hash,val);
	rb_hash_aset(hash, key, converted);
    return converted;
}

#strhashObject



340
341
342
343
344
# File 'ext/hwia/hwia.c', line 340

static VALUE
rb_strhash_strhash(VALUE hash)
{
	return hash;
}

#stringify_keys!Object



9
# File 'lib/hwia_rails.rb', line 9

def stringify_keys!; self end

#symbolize_keys!Object



10
# File 'lib/hwia_rails.rb', line 10

def symbolize_keys!; self end

#to_hashObject



486
487
488
489
490
491
492
# File 'ext/hwia/hwia.c', line 486

static VALUE
rb_strhash_to_hash(VALUE hash){
	VALUE hsh = rb_hash_update(rb_hash_new(), hash);
	RHASH(hsh)->ifnone = RHASH(hash)->ifnone;
	rb_hash_foreach(hsh, rb_strhash_to_hash_i, hsh);
	return hsh;
}

#to_options!Object



11
# File 'lib/hwia_rails.rb', line 11

def to_options!; self end

#update(hash2) ⇒ Object

hash.c



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'ext/hwia/hwia.c', line 384

static VALUE
rb_strhash_update(VALUE hash1, VALUE hash2)
{
#ifdef RUBY19	
	rb_hash_modify(hash1);
#endif
    hash2 = to_strhash(hash2);
    if (rb_block_given_p()) {
	rb_hash_foreach(hash2, rb_strhash_update_block_i, hash1);
    }
    else {
	rb_hash_foreach(hash2, rb_strhash_update_i, hash1);
    }
    return hash1;
}