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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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;
}