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.



565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'opal/stdlib/native.rb', line 565

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

        for (key in defaults) {
          value = defaults[key];
          $hash_convert_and_put_value(self, key, value);
        }

        return self;
      } else if (defaults instanceof Map) {
        Opal.hash_each(defaults, false, function(key, value) {
          $hash_convert_and_put_value(self, key, value);
          return [false, false];
        });
      }
    }

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

Instance Method Details

#_initializeObject



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

alias _initialize initialize

#pretty_print(q) ⇒ Object

:nodoc:



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

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

#pretty_print_cycle(q) ⇒ Object

:nodoc:



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

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



156
157
158
159
160
161
162
163
164
165
166
167
# File 'opal/stdlib/json.rb', line 156

def to_json
  %x{
    var result = [];

    Opal.hash_each(self, false, function(key, value) {
      result.push(#{`key`.to_s.to_json} + ':' + #{`value`.to_json});
      return [false, false];
    });

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

#to_nObject

all keys and values.

Returns:

  • a JavaScript Map but calling #to_n on



592
593
594
595
596
597
598
599
600
601
602
603
# File 'opal/stdlib/native.rb', line 592

def to_n
  %x{
    var result = new Map();

    Opal.hash_each(self, false, function(key, value) {
      result.set(#{Native.try_convert(`key`, `key`)} , #{Native.try_convert(`value`, `value`)});
      return [false, false];
    });

    return result;
  }
end