Class: Hash

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/opal/corelib/hash.rb,
opal/opal/corelib/marshal/write_buffer.rb,
opal/opal/corelib/pattern_matching/base.rb

Overview


Internal properties:

  • $$keys [Map] optional Map of key arrays, used when objects are used as keys
  • $$proc [Proc,null,nil] the default proc used for missing keys
  • key-array [JS::Map] an element of a array that holds objects used as keys, { key_hash => [objects...] }

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chunk, #chunk_while, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #enumerator_size, #filter_map, #find_all, #find_index, #first, #grep, #grep_v, #group_by, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reverse_each, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_set, #uniq, #zip

Constructor Details

#initialize(defaults = undefined, &block) ⇒ Hash

Returns a new instance of Hash.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'opal/opal/corelib/hash.rb', line 81

def initialize(defaults = undefined, &block)
  %x{
    $deny_frozen_access(self);

    if (defaults !== undefined && block !== nil) {
      #{::Kernel.raise ::ArgumentError, 'wrong number of arguments (1 for 0)'}
    }
    self.$$none = (defaults === undefined ? nil : defaults);
    self.$$proc = block;

    return self;
  }
end

Class Method Details

.[](*argv) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'opal/opal/corelib/hash.rb', line 18

def self.[](*argv)
  %x{
    var hash, argc = argv.length, arg, i;

    if (argc === 1) {
      hash = #{::Opal.coerce_to?(argv[0], ::Hash, :to_hash)};
      if (hash !== nil) {
        return #{allocate.merge!(`hash`)};
      }

      argv = #{::Opal.coerce_to?(argv[0], ::Array, :to_ary)};
      if (argv === nil) {
        #{::Kernel.raise ::ArgumentError, 'odd number of arguments for Hash'};
      }

      argc = argv.length;
      hash = #{allocate};

      for (i = 0; i < argc; i++) {
        arg = argv[i];
        if (!arg.$$is_array)
          #{::Kernel.raise ::ArgumentError, "invalid element #{`arg`.inspect} for Hash"};
        if (arg.length === 1) {
          hash.$store(arg[0], nil);
        } else if (arg.length === 2) {
          hash.$store(arg[0], arg[1]);
        } else {
          #{::Kernel.raise ::ArgumentError, "invalid number of elements (#{`arg.length`} for #{`arg`.inspect}), must be 1..2"};
        }
      }

      return hash;
    }

    if (argc % 2 !== 0) {
      #{::Kernel.raise ::ArgumentError, 'odd number of arguments for Hash'}
    }

    hash = #{allocate};

    for (i = 0; i < argc; i += 2) {
      hash.$store(argv[i], argv[i + 1]);
    }

    return hash;
  }
end

.allocateObject



66
67
68
69
70
71
72
73
74
75
# File 'opal/opal/corelib/hash.rb', line 66

def self.allocate
  %x{
    var hash = new self.$$constructor();

    hash.$$none = nil;
    hash.$$proc = nil;

    return hash;
  }
end

.try_convert(obj) ⇒ Object



77
78
79
# File 'opal/opal/corelib/hash.rb', line 77

def self.try_convert(obj)
  ::Opal.coerce_to?(obj, ::Hash, :to_hash)
end

Instance Method Details

#<(other) ⇒ Object



156
157
158
159
# File 'opal/opal/corelib/hash.rb', line 156

def <(other)
  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)
  other > self
end

#<=(other) ⇒ Object



161
162
163
164
# File 'opal/opal/corelib/hash.rb', line 161

def <=(other)
  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)
  other >= self
end

#==(other) ⇒ Object Also known as: eql?



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'opal/opal/corelib/hash.rb', line 95

def ==(other)
  %x{
    if (self === other) {
      return true;
    }

    if (!other.$$is_hash) {
      return false;
    }

    if (self.size !== other.size) {
      return false;
    }

    return $hash_each(self, true, function(key, value) {
      var other_value = $hash_get(other, key);
      if (other_value === undefined || !value['$eql?'](other_value)) {
        return [true, false];
      }
      return [false, true];
    });
  }
end

#>(other) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'opal/opal/corelib/hash.rb', line 144

def >(other)
  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)

  %x{
    if (self.size <= other.size) {
      return false;
    }
  }

  self >= other
end

#>=(other) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'opal/opal/corelib/hash.rb', line 119

def >=(other)
  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)

  %x{
    if (self.size < other.size) {
      return false;
    }
  }

  result = true

  other.each do |other_key, other_val|
    val = fetch(other_key, `null`)

    %x{
      if (val == null || val !== other_val) {
        result = false;
        return;
      }
    }
  end

  result
end

#[](key) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'opal/opal/corelib/hash.rb', line 166

def [](key)
  %x{
    var value = $hash_get(self, key);

    if (value !== undefined) {
      return value;
    }

    return self.$default(key);
  }
end

#[]=(key, value) ⇒ Object Also known as: store



178
179
180
181
182
183
184
185
# File 'opal/opal/corelib/hash.rb', line 178

def []=(key, value)
  %x{
    $deny_frozen_access(self);

    $hash_put(self, key, value);
    return value;
  }
end

#__marshal__(buffer) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 63

def __marshal__(buffer)
  if default_proc
    ::Kernel.raise ::TypeError, "can't dump hash with default proc"
  end

  buffer.save_link(self)
  buffer.write_ivars_prefix(self)
  buffer.write_extends(self)
  buffer.write_user_class(Hash, self)
  if default
    buffer.append('}')
    buffer.write_hash(self)
    buffer.write(default)
  else
    buffer.append('{')
    buffer.write_hash(self)
  end
  buffer.write_ivars_suffix(self)
end

#assoc(object) ⇒ Object



187
188
189
190
191
192
193
194
195
196
# File 'opal/opal/corelib/hash.rb', line 187

def assoc(object)
  %x{
    return $hash_each(self, nil, function(key, value) {
      if (#{`key` == object}) {
        return [true, [key, value]];
      }
      return [false, nil];
    });
  }
end

#clearObject



198
199
200
201
202
203
204
205
206
207
208
# File 'opal/opal/corelib/hash.rb', line 198

def clear
  %x{
    $deny_frozen_access(self);

    self.clear();
    if (self.$$keys)
      self.$$keys.clear();

    return self;
  }
end

#cloneObject



210
211
212
213
214
215
216
# File 'opal/opal/corelib/hash.rb', line 210

def clone
  %x{
    var hash = self.$class().$new();
    $hash_clone(self, hash);
    return self["$frozen?"]() ? hash.$freeze() : hash;
  }
end

#compactObject



218
219
220
221
222
223
224
225
226
227
228
229
# File 'opal/opal/corelib/hash.rb', line 218

def compact
  %x{
    var hash = new Map();

    return $hash_each(self, hash, function(key, value) {
      if (value !== nil) {
        $hash_put(hash, key, value);
      }
      return [false, hash];
    });
  }
end

#compact!Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'opal/opal/corelib/hash.rb', line 231

def compact!
  %x{
    $deny_frozen_access(self);

    var result = nil;

    return $hash_each(self, result, function(key, value) {
      if (value === nil) {
        $hash_delete(self, key);
        result = self;
      }
      return [false, result];
    });
  }
end

#compare_by_identityObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'opal/opal/corelib/hash.rb', line 247

def compare_by_identity
  %x{
    $deny_frozen_access(self);

    if (!self.$$by_identity) {
      self.$$by_identity = true;

      if (self.size !== 0)
        Opal.hash_rehash(self);
    }

    return self;
  }
end

#compare_by_identity?Boolean

Returns:



262
263
264
# File 'opal/opal/corelib/hash.rb', line 262

def compare_by_identity?
  `self.$$by_identity === true`
end

#deconstruct_keys(_) ⇒ Object



8
9
10
# File 'opal/opal/corelib/pattern_matching/base.rb', line 8

def deconstruct_keys(_)
  self
end

#default(key = undefined) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
# File 'opal/opal/corelib/hash.rb', line 266

def default(key = undefined)
  %x{
    if (key !== undefined && self.$$proc !== nil && self.$$proc !== undefined) {
      return self.$$proc.$call(self, key);
    }
    if (self.$$none === undefined) {
      return nil;
    }
    return self.$$none;
  }
end

#default=(object) ⇒ Object



278
279
280
281
282
283
284
285
286
287
# File 'opal/opal/corelib/hash.rb', line 278

def default=(object)
  %x{
    $deny_frozen_access(self);

    self.$$proc = nil;
    self.$$none = object;

    return object;
  }
end

#default_procObject



289
290
291
292
293
294
295
296
# File 'opal/opal/corelib/hash.rb', line 289

def default_proc
  %x{
    if (self.$$proc !== undefined) {
      return self.$$proc;
    }
    return nil;
  }
end

#default_proc=(default_proc) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'opal/opal/corelib/hash.rb', line 298

def default_proc=(default_proc)
  %x{
    $deny_frozen_access(self);

    var proc = default_proc;

    if (proc !== nil) {
      proc = #{::Opal.coerce_to!(`proc`, ::Proc, :to_proc)};

      if (#{`proc`.lambda?} && #{`proc`.arity.abs} !== 2) {
        #{::Kernel.raise ::TypeError, 'default_proc takes two arguments'};
      }
    }

    self.$$none = nil;
    self.$$proc = proc;

    return default_proc;
  }
end

#delete(key, &block) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'opal/opal/corelib/hash.rb', line 319

def delete(key, &block)
  %x{
    $deny_frozen_access(self);
    var value = $hash_delete(self, key);

    if (value !== undefined) {
      return value;
    }

    if (block !== nil) {
      return #{yield key};
    }

    return nil;
  }
end

#delete_if(&block) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'opal/opal/corelib/hash.rb', line 336

def delete_if(&block)
  return enum_for(:delete_if) { size } unless block

  %x{
    $deny_frozen_access(self);

    return $hash_each(self, self, function(key, value) {
      var obj = block(key, value);

      if (obj !== false && obj !== nil) {
        $hash_delete(self, key);
      }
      return [false, self];
    });
  }
end

#dig(key, *keys) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'opal/opal/corelib/hash.rb', line 353

def dig(key, *keys)
  item = self[key]

  %x{
    if (item === nil || keys.length === 0) {
      return item;
    }
  }

  unless item.respond_to?(:dig)
    ::Kernel.raise ::TypeError, "#{item.class} does not have #dig method"
  end

  item.dig(*keys)
end

#dupObject



369
370
371
# File 'opal/opal/corelib/hash.rb', line 369

def dup
  `$hash_clone(self, self.$class().$new())`
end

#each(&block) ⇒ Object Also known as: each_pair



373
374
375
376
377
378
379
380
381
382
# File 'opal/opal/corelib/hash.rb', line 373

def each(&block)
  return enum_for(:each) { size } unless block

  %x{
    return $hash_each(self, self, function(key, value) {
      $yield1(block, [key, value]);
      return [false, self];
    });
  }
end

#each_key(&block) ⇒ Object



384
385
386
387
388
389
390
391
392
393
# File 'opal/opal/corelib/hash.rb', line 384

def each_key(&block)
  return enum_for(:each_key) { size } unless block

  %x{
    return $hash_each(self, self, function(key, value) {
      block(key);
      return [false, self];
    });
  }
end

#each_value(&block) ⇒ Object



395
396
397
398
399
400
401
402
403
404
# File 'opal/opal/corelib/hash.rb', line 395

def each_value(&block)
  return enum_for(:each_value) { size } unless block

  %x{
    return $hash_each(self, self, function(key, value) {
      block(value);
      return [false, self];
    });
  }
end

#empty?Boolean

Returns:



406
407
408
# File 'opal/opal/corelib/hash.rb', line 406

def empty?
  `self.size === 0`
end

#except(*keys) ⇒ Object



410
411
412
# File 'opal/opal/corelib/hash.rb', line 410

def except(*keys)
  dup.except!(*keys)
end

#except!(*keys) ⇒ Object



414
415
416
417
# File 'opal/opal/corelib/hash.rb', line 414

def except!(*keys)
  keys.each { |key| delete(key) }
  self
end

#fetch(key, defaults = undefined, &block) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'opal/opal/corelib/hash.rb', line 419

def fetch(key, defaults = undefined, &block)
  %x{
    var value = $hash_get(self, key);

    if (value !== undefined) {
      return value;
    }

    if (block !== nil) {
      return block(key);
    }

    if (defaults !== undefined) {
      return defaults;
    }
  }

  ::Kernel.raise ::KeyError.new("key not found: #{key.inspect}", key: key, receiver: self)
end

#fetch_values(*keys, &block) ⇒ Object



439
440
441
# File 'opal/opal/corelib/hash.rb', line 439

def fetch_values(*keys, &block)
  keys.map { |key| fetch(key, &block) }
end

#flatten(level = 1) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'opal/opal/corelib/hash.rb', line 443

def flatten(level = 1)
  level = ::Opal.coerce_to!(level, ::Integer, :to_int)

  %x{
    var result = [];

    return $hash_each(self, result, function(key, value) {
      result.push(key);

      if (value.$$is_array) {
        if (level === 1) {
          result.push(value);
          return [false, result];
        }

        result = result.concat(#{`value`.flatten(`level - 2`)});
        return [false, result];
      }

      result.push(value);
      return [false, result];
    });
  }
end

#freezeObject



468
469
470
471
472
# File 'opal/opal/corelib/hash.rb', line 468

def freeze
  return self if frozen?

  `$freeze(self)`
end

#has_key?(key) ⇒ Boolean Also known as: include?, key?, member?

Returns:



474
475
476
# File 'opal/opal/corelib/hash.rb', line 474

def has_key?(key)
  `$hash_get(self, key) !== undefined`
end

#has_value?(value) ⇒ Boolean Also known as: value?

Returns:



478
479
480
481
482
483
484
485
486
487
# File 'opal/opal/corelib/hash.rb', line 478

def has_value?(value)
  %x{
    return $hash_each(self, false, function(key, val) {
      if (#{`val` == value}) {
        return [true, true];
      }
      return [false, false];
    });
  }
end

#hashObject



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'opal/opal/corelib/hash.rb', line 491

def hash
  %x{
    var top = ($hash_ids === undefined),
        hash_id = self.$object_id(),
        result = $opal32_init(),
        key, item, i,
        size = self.size, ary = new Int32Array(size);

    result = $opal32_add(result, 0x4);
    result = $opal32_add(result, size);

    if (top) {
      $hash_ids = Object.create(null);
    }
    else if ($hash_ids[hash_id]) {
      return $opal32_add(result, 0x01010101);
    }

    try {
      for (key in $hash_ids) {
        item = $hash_ids[key];
        if (#{eql?(`item`)}) {
          return $opal32_add(result, 0x01010101);
        }
      }

      $hash_ids[hash_id] = self;
      i = 0

      $hash_each(self, false, function(key, value) {
        ary[i] = [0x70414952, key, value].$hash();
        i++;
        return [false, false];
      });

      ary = ary.sort();

      for (i = 0; i < ary.length; i++) {
        result = $opal32_add(result, ary[i]);
      }

      return result;
    } finally {
      if (top) {
        $hash_ids = undefined;
      }
    }
  }
end

#index(object) ⇒ Object Also known as: key



541
542
543
544
545
546
547
548
549
550
# File 'opal/opal/corelib/hash.rb', line 541

def index(object)
  %x{
    return $hash_each(self, nil, function(key, value) {
      if (#{`value` == object}) {
        return [true, key];
      }
      return [false, nil];
    });
  }
end

#indexes(*args) ⇒ Object Also known as: indices, values_at



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'opal/opal/corelib/hash.rb', line 552

def indexes(*args)
  %x{
    var result = [];

    for (var i = 0, length = args.length, key, value; i < length; i++) {
      key = args[i];
      value = $hash_get(self, key);

      if (value === undefined) {
        result.push(#{default});
        continue;
      }

      result.push(value);
    }

    return result;
  }
end

#inspectObject Also known as: to_s



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'opal/opal/corelib/hash.rb', line 574

def inspect
  %x{
    var top = (inspect_ids === undefined),
        hash_id = self.$object_id(),
        result = [];
  }

  begin
    %x{
      if (top) {
        inspect_ids = {};
      }

      if (inspect_ids.hasOwnProperty(hash_id)) {
        return '{...}';
      }

      inspect_ids[hash_id] = true;

      $hash_each(self, false, function(key, value) {
        value = #{Opal.inspect(`value`)}
        key = #{Opal.inspect(`key`)}

        result.push(key + '=>' + value);
        return [false, false];
      })

      return '{' + result.join(', ') + '}';
    }
    nil
  ensure
    `if (top) inspect_ids = undefined`
  end
end

#invertObject



609
610
611
612
613
614
615
616
617
618
# File 'opal/opal/corelib/hash.rb', line 609

def invert
  %x{
    var hash = new Map();

    return $hash_each(self, hash, function(key, value) {
      $hash_put(hash, value, key);
      return [false, hash];
    });
  }
end

#keep_if(&block) ⇒ Object



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'opal/opal/corelib/hash.rb', line 620

def keep_if(&block)
  return enum_for(:keep_if) { size } unless block

  %x{
    $deny_frozen_access(self);

    return $hash_each(self, self, function(key, value) {
      var obj = block(key, value);

      if (obj === false || obj === nil) {
        $hash_delete(self, key);
      }
      return [false, self];
    });
  }
end

#keysObject



637
638
639
# File 'opal/opal/corelib/hash.rb', line 637

def keys
  `Array.from(self.keys())`
end

#lengthObject Also known as: size



641
642
643
# File 'opal/opal/corelib/hash.rb', line 641

def length
  `self.size`
end

#merge(*others, &block) ⇒ Object



645
646
647
# File 'opal/opal/corelib/hash.rb', line 645

def merge(*others, &block)
  dup.merge!(*others, &block)
end

#merge!(*others, &block) ⇒ Object Also known as: update



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'opal/opal/corelib/hash.rb', line 649

def merge!(*others, &block)
  %x{
    $deny_frozen_access(self);

    var i, j, other;
    for (i = 0; i < others.length; ++i) {
      other = #{::Opal.coerce_to!(`others[i]`, ::Hash, :to_hash)};

      if (block === nil) {
        $hash_each(other, false, function(key, value) {
          $hash_put(self, key, value);
          return [false, false];
        });
      } else {
        $hash_each(other, false, function(key, value) {
          var val = $hash_get(self, key);

          if (val === undefined) {
            $hash_put(self, key, value);
            return [false, false];
          }

          $hash_put(self, key, block(key, val, value));
          return [false, false];
        });
      }
    }

    return self;
  }
end

#rassoc(object) ⇒ Object



681
682
683
684
685
686
687
688
689
690
# File 'opal/opal/corelib/hash.rb', line 681

def rassoc(object)
  %x{
    return $hash_each(self, nil, function(key, value) {
      if (#{`value` == object}) {
        return [true, [key, value]];
      }
      return [false, nil];
    });
  }
end

#rehashObject



692
693
694
695
696
697
# File 'opal/opal/corelib/hash.rb', line 692

def rehash
  %x{
    $deny_frozen_access(self);
    return Opal.hash_rehash(self);
  }
end

#reject(&block) ⇒ Object



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'opal/opal/corelib/hash.rb', line 699

def reject(&block)
  return enum_for(:reject) { size } unless block

  %x{
    var hash = new Map();

    return $hash_each(self, hash, function(key, value) {
      var obj = block(key, value);

      if (obj === false || obj === nil) {
        $hash_put(hash, key, value);
      }
      return [false, hash]
    });
  }
end

#reject!(&block) ⇒ Object



716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'opal/opal/corelib/hash.rb', line 716

def reject!(&block)
  return enum_for(:reject!) { size } unless block

  %x{
    $deny_frozen_access(self);

    var result = nil;

    return $hash_each(self, result, function(key, value) {
      var obj = block(key, value);

      if (obj !== false && obj !== nil) {
        $hash_delete(self, key);
        result = self;
      }
      return [false, result];
    });
  }
end

#replace(other) ⇒ Object



736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'opal/opal/corelib/hash.rb', line 736

def replace(other)
  `$deny_frozen_access(self);`

  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)

  %x{
    self.$clear();

    $hash_each(other, false, function(key, value) {
      $hash_put(self, key, value);
      return [false, false];
    });
  }

  if other.default_proc
    self.default_proc = other.default_proc
  else
    self.default = other.default
  end

  self
end

#select(&block) ⇒ Object Also known as: filter



759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'opal/opal/corelib/hash.rb', line 759

def select(&block)
  return enum_for(:select) { size } unless block

  %x{
    var hash = new Map();

    return $hash_each(self, hash, function(key, value) {
      var obj = block(key, value);

      if (obj !== false && obj !== nil) {
        $hash_put(hash, key, value);
      }
      return [false, hash];
    });
  }
end

#select!(&block) ⇒ Object Also known as: filter!



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'opal/opal/corelib/hash.rb', line 776

def select!(&block)
  return enum_for(:select!) { size } unless block

  %x{
    $deny_frozen_access(self);

    var result = nil;

    return $hash_each(self, result, function(key, value) {
      var obj = block(key, value);

      if (obj === false || obj === nil) {
        $hash_delete(self, key);
        result = self;
      }
      return [false, result];
    });
  }
end

#shiftObject



796
797
798
799
800
801
802
803
804
# File 'opal/opal/corelib/hash.rb', line 796

def shift
  %x{
    $deny_frozen_access(self);

    return $hash_each(self, nil, function(key, value) {
      return [true, [key, $hash_delete(self, key)]];
    });
  }
end

#slice(*keys) ⇒ Object



806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'opal/opal/corelib/hash.rb', line 806

def slice(*keys)
  %x{
    var result = new Map();

    for (var i = 0, length = keys.length; i < length; i++) {
      var key = keys[i], value = $hash_get(self, key);

      if (value !== undefined) {
        $hash_put(result, key, value);
      }
    }

    return result;
  }
end

#to_aObject



822
823
824
825
826
827
828
829
830
831
# File 'opal/opal/corelib/hash.rb', line 822

def to_a
  %x{
    var result = [];

    return $hash_each(self, result, function(key, value) {
      result.push([key, value]);
      return [false, result];
    });
  }
end

#to_h(&block) ⇒ Object



833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'opal/opal/corelib/hash.rb', line 833

def to_h(&block)
  return map(&block).to_h if block_given?

  %x{
    if (self.$$class === Opal.Hash) {
      return self;
    }

    var hash = new Map();

    $hash_clone(self, hash);

    return hash;
  }
end

#to_hashObject



849
850
851
# File 'opal/opal/corelib/hash.rb', line 849

def to_hash
  self
end

#to_procObject



853
854
855
856
857
858
859
860
861
862
863
# File 'opal/opal/corelib/hash.rb', line 853

def to_proc
  proc do |key = undefined|
    %x{
      if (key == null) {
        #{::Kernel.raise ::ArgumentError, 'no key given'}
      }
    }

    self[key]
  end
end

#transform_keys(keys_hash = nil, &block) ⇒ Object



865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# File 'opal/opal/corelib/hash.rb', line 865

def transform_keys(keys_hash = nil, &block)
  return enum_for(:transform_keys) { size } if !block && !keys_hash

  %x{
    var result = new Map();

    return $hash_each(self, result, function(key, value) {
      var new_key;
      if (keys_hash !== nil)
        new_key = $hash_get(keys_hash, key);
      if (new_key === undefined && block && block !== nil)
        new_key = block(key);
      if (new_key === undefined)
        new_key = key // key not modified
      $hash_put(result, new_key, value);
      return [false, result];
    });
  }
end

#transform_keys!(keys_hash = nil, &block) ⇒ Object



885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# File 'opal/opal/corelib/hash.rb', line 885

def transform_keys!(keys_hash = nil, &block)
  return enum_for(:transform_keys!) { size } if !block && !keys_hash

  %x{
    $deny_frozen_access(self);

    var modified_keys = new Map();

    return $hash_each(self, self, function(key, value) {
      var new_key;
      if (keys_hash !== nil)
        new_key = $hash_get(keys_hash, key);
      if (new_key === undefined && block && block !== nil)
        new_key = block(key);
      if (new_key === undefined)
        return [false, self]; // key not modified
      if (!$hash_get(modified_keys, key))
        $hash_delete(self, key);
      $hash_put(self, new_key, value);
      $hash_put(modified_keys, new_key, true)
      return [false, self];
    });
  }
end

#transform_values(&block) ⇒ Object



910
911
912
913
914
915
916
917
918
919
920
921
# File 'opal/opal/corelib/hash.rb', line 910

def transform_values(&block)
  return enum_for(:transform_values) { size } unless block

  %x{
    var result = new Map();

    return $hash_each(self, result, function(key, value) {
      $hash_put(result, key, block(value));
      return [false, result];
    });
  }
end

#transform_values!(&block) ⇒ Object



923
924
925
926
927
928
929
930
931
932
933
934
# File 'opal/opal/corelib/hash.rb', line 923

def transform_values!(&block)
  return enum_for(:transform_values!) { size } unless block

  %x{
    $deny_frozen_access(self);

    return $hash_each(self, self, function(key, value) {
      $hash_put(self, key, block(value));
      return [false, self];
    });
  }
end

#valuesObject



936
937
938
# File 'opal/opal/corelib/hash.rb', line 936

def values
  `Array.from(self.values())`
end