Class: Hash

Inherits:
Object show all
Defined in:
opal/stdlib/pp.rb,
opal/stdlib/json.rb,
opal/stdlib/native.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Hash.



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'opal/stdlib/native.rb', line 538

def initialize(defaults = undefined, &block)
  %x{
    if (defaults != null &&
         (defaults.constructor === undefined ||
           defaults.constructor === Object)) {
      var smap = self.$$smap,
          keys = self.$$keys,
          key, value;

      for (key in defaults) {
        value = defaults[key];

        if (value &&
             (value.constructor === undefined ||
               value.constructor === Object)) {
          smap[key] = #{Hash.new(`value`)};
        } else if (value && value.$$is_array) {
          value = value.map(function(item) {
            if (item &&
                 (item.constructor === undefined ||
                   item.constructor === Object)) {
              return #{Hash.new(`item`)};
            }

            return #{Native(`item`)};
          });
          smap[key] = value
        } else {
          smap[key] = #{Native(`value`)};
        }

        keys.push(key);
      }

      return self;
    }

    return #{_initialize(defaults, &block)};
  }
end

Instance Method Details

#_initializeObject



536
# File 'opal/stdlib/native.rb', line 536

alias _initialize initialize

#pretty_print(q) ⇒ Object

:nodoc:



365
366
367
# File 'opal/stdlib/pp.rb', line 365

def pretty_print(q) # :nodoc:
  q.pp_hash self
end

#pretty_print_cycle(q) ⇒ Object

:nodoc:



369
370
371
# File 'opal/stdlib/pp.rb', line 369

def pretty_print_cycle(q) # :nodoc:
  q.text(empty? ? '{}' : '{...}')
end

#ruby2_keywords_hash(hash) ⇒ Object

call-seq: Hash.ruby2_keywords_hash(hash) -> new_hash

Duplicates a given hash and returns the new hash.



56
57
58
# File 'opal/stdlib/ruby2_keywords.rb', line 56

def ruby2_keywords_hash(hash)
  hash.dup
end

#ruby2_keywords_hash?(hash) ⇒ Boolean

call-seq: Hash.ruby2_keywords_hash?(hash) -> false

Returns false.

Returns:



46
47
48
# File 'opal/stdlib/ruby2_keywords.rb', line 46

def ruby2_keywords_hash?(hash)
  false
end

#to_jsonObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'opal/stdlib/json.rb', line 154

def to_json
  %x{
    var result = [];

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

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      result.push(#{`key`.to_s.to_json} + ':' + #{`value`.to_json});
    }

    return '{' + result.join(',') + '}';
  }
end

#to_nObject

all values.

Returns:

  • a JavaScript object with the same keys but calling #to_n on



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'opal/stdlib/native.rb', line 581

def to_n
  %x{
    var result = {},
        keys = self.$$keys,
        smap = self.$$smap,
        key, value;

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

      if (key.$$is_string) {
        value = smap[key];
      } else {
        key = key.key;
        value = key.value;
      }

      result[key] = #{Native.try_convert(`value`, `value`)};
    }

    return result;
  }
end