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



374
375
376
377
378
379
380
381
382
383
# File 'ext/hwia/hwia.c', line 374

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



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

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



278
279
280
281
282
283
# File 'ext/hwia/hwia.c', line 278

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

#dupObject

revist, same API, but may be clobbered



318
319
320
321
322
323
324
# File 'ext/hwia/hwia.c', line 318

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



385
386
387
388
389
# File 'ext/hwia/hwia.c', line 385

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

#merge!(hash2) ⇒ Object

hash.c



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'ext/hwia/hwia.c', line 358

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'ext/hwia/hwia.c', line 236

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



278
279
280
281
282
283
# File 'ext/hwia/hwia.c', line 278

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

#strhashObject



312
313
314
315
316
# File 'ext/hwia/hwia.c', line 312

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



461
462
463
464
465
466
467
# File 'ext/hwia/hwia.c', line 461

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



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'ext/hwia/hwia.c', line 358

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