Class: Hash

Inherits:
Object show all
Defined in:
lib/source/ruby.rb

Overview

A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done not by an integer index but by arbitrary keys of any object type.

Hashes have a default value that is returned when accessing keys that do not exist in the hash, which is nil unless otherwise assigned.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Hash

call-seq:

Hash.new                      -> hash
Hash.new(obj)                 -> hash
Hash.new { |hash, key| block } -> hash

Returns a new, empty hash. If this hash is subsequently accessed with a key that doesn’t correspond to an existing hash entry, the value returned depends on the style of new used to create the hash. In the first form, the access returns nil. If obj is specified, this single object will be used for all default values. If a block is specified, it will be called with the hash object and the key, and should return the default value. It is the block’s responsibility to store the value in the hash if required.

h1 = Hash.new("No value for that key")

h1[:a] = 100      #=> 100
h1[:b] = 200      #=> 200
h1[:a]            #=> 100
h1[:c]            #=> "No value for that key"
# The following alters the single default object
h1[:c].upcase!    #=> "NO VALUE FOR THAT KEY"
h1[:d]            #=> "NO VALUE FOR THAT KEY"
# Values from defaults are not stored in the hash
h1.keys           #=> [:a, :b]

h2 = Hash.new { |hash, key| hash[key] = "No value at #{key}" }

h2[:c]            #=> "No value at c"
# A new object is created by the default block each time
h2[:c].upcase!    #=> "NO VALUE AT C"
h2[:d]            #=> "No value at d"
# Values from defaults are stored in the hash
h2.keys           #=> [:c, :d]


3087
3088
3089
3090
# File 'lib/source/ruby.rb', line 3087

def initialize(&block)
  `this._default=(block==null?nil:block)`
  `this._contents={}`
end

Class Method Details

.[](*args) ⇒ Object

call-seq:

Hash[ [key (, | =>) value]* ] -> hash

Creates a new hash populated with the given objects. Equivalent to the literal {key, value, ...}. Keys and values occur in pairs, so there must be an even number of arguments.

Hash[:a, 100, 'b', 200]        #=> {:a => 100, "b" => 200}
Hash[:a => 100, 'b' => 200]    #=> {:a => 100, "b" => 200}
{:a => 100, 'b' => 200}        #=> {:a => 100, "b" => 200}


3047
3048
3049
3050
3051
# File 'lib/source/ruby.rb', line 3047

def self.[](*args)
  `if(args.length==1&&args[0].m$class()==c$Hash){return args[0];}`
  `for(var i=0,l=args.length,result=c$Hash.m$new(),c=result._contents;i<l;i+=2){var k=args[i],v=args[i+1],h=k.m$hash();c[h]=[k,v]}`
  return `result`
end

Instance Method Details

#==(other) ⇒ Object

call-seq:

hsh == other -> true or false

Equality – two hashes are equal if they contain the same number of keys and if for every key, hsh[key] == other[key].

h1 = {:a => 1, :b => 2}
h2 = {7 => 35, :b => 2, :a => 1}
h3 = {:a => 1, :b => 2, 7 => 35}
h4 = {:a => 1, :c => 2, :f => 35}

h1 == h2    #=> false
h2 == h3    #=> true
h3 == h4    #=> false


3107
3108
3109
3110
3111
3112
# File 'lib/source/ruby.rb', line 3107

def ==(other)
  `var c=this._contents,o=other._contents`
  `for(var x in o){if(x.slice(1,2)=='_'&&c[x]==null){return false;};}`
  `for(var x in c){if(x.slice(1,2)=='_'&&!c[x][1].m$_eql2(o[x][1])){return false;};}`
  return true
end

#[](k) ⇒ Object

call-seq:

hsh[key] -> value

Element Reference – retrieves the value object corresponding to the key object. If not found, returns the default value (see Hash::new for details).

h = {:a => 100, :b => 200}

h[:a]   => 100
h[:c]   => nil


3126
3127
3128
3129
3130
# File 'lib/source/ruby.rb', line 3126

def [](k)
  `var kv=this._contents[k.m$hash()]`
  `if(!kv){var d=this._default;return(typeof(d)=='function'?d(this,kv[0]):d);}`
  return `kv[1]`
end

#[]=(k, v) ⇒ Object

call-seq:

hsh[key] = value     -> value
hsh.store(key,value) -> value

Element Assignment – associates the value given by value with the key given by key. The object key should not have its value changed while it is in use as a key.

h = {:a => 100, :b => 200}

h[:a] = 150   #=> 150
h[:c] = 300   #=> 300
h             #=> {:a => 150, :b => 200, :c => 300}


3146
3147
3148
3149
# File 'lib/source/ruby.rb', line 3146

def []=(k,v)
  `this._contents[k.m$hash()]=[k,v]`
  return `v`
end

#clearObject

call-seq:

hsh.clear -> hsh

Removes all key-value pairs from hsh.

h = {:a => 1, :b => 2}

h.clear   #=> {}
h         #=> {}


3161
3162
3163
3164
# File 'lib/source/ruby.rb', line 3161

def clear
  `this._contents={}`
  return self
end

#default(key = nil) ⇒ Object

call-seq:

hsh.default(key = nil) -> obj

Returns the default value, the value that would be returned by hsh[key] if key did not exist in hsh. See also Hash::new and Hash#default=.

h = Hash.new                              #=> {}
h.default                                 #=> nil
h.default(2)                              #=> nil

h = Hash.new('FAIL')                      #=> {}
h.default                                 #=> 'FAIL'
h.default(2)                              #=> 'FAIL'

h = Hash.new {|h,k| h[k] = k.to_i * 10}   #=> {}
h.default                                 #=> 0
h.default(2)                              #=> 20


3185
3186
3187
3188
# File 'lib/source/ruby.rb', line 3185

def default(key = nil)
  `var d=this._default`
  return `typeof(d)=='function'?d(this,key):d`
end

#default=(obj) ⇒ Object

call-seq:

hsh.default = obj -> hsh

Sets the default value, the value returned for a key that does not exist in the hash. It is not possible to set the a default to a Proc that will be executed on each key lookup.

h = {:a => 100, :b => 200}
h.default = 'Nothing'

h[:a]       #=> 100
h[:z]       #=> 'Nothing'

# This doesn't do what you might hope...
h.default = proc {|h,k| h[k] = k + k }

h[2]        #=> #<Proc:201>
h['foo']    #=> #<Proc:201>


3209
3210
3211
3212
# File 'lib/source/ruby.rb', line 3209

def default=(obj)
  `this._default=obj`
  return self
end

#default_procObject

call-seq:

hsh.default_proc -> proc

If Hash::new was invoked with a block, return that block, otherwise return nil.

h = Hash.new {|h,k| h[k] = k*k }   #=> {}
p = h.default_proc                 #=> #<Proc:201>
a = []                             #=> []
p.call(a, 2)
a                                  #=> [nil, nil, 4]


3226
3227
3228
3229
# File 'lib/source/ruby.rb', line 3226

def default_proc
  `var d=this._default`
  return `typeof(d)=='function'?c$Proc.m$new(d):nil`
end

#delete(k) ⇒ Object

call-seq:

hsh.delete(key)                 -> value
hsh.delete(key) { |key| block } -> value

Deletes and returns a key-value pair from hsh whose key is equal to key. If the key is not found, returns the default value, or if the optional code block is given, pass in the requested key and return the result of block.

h = {:a => 100, :b => 200}

h.delete(:a)                                    #=> 100
h.delete(:z)                                    #=> nil
h.delete(:z) {|k| "#{k.inspect} not found" }    #=> ":z not found"


3246
3247
3248
3249
3250
# File 'lib/source/ruby.rb', line 3246

def delete(k)
  `var c=this._contents,d=this._default,x=k.m$hash(),kv=c[x]`
  `if(kv!=null){var result=kv[1];delete(c[x]);return result;}`
  return `typeof(_block)=='function'?#{yield `k`}:(typeof(d)=='function'?d(this,k):d)`
end

#delete_ifObject

call-seq:

hsh.delete_if { |key, value| block } -> hsh

Deletes every key-value pair from hsh for which block evaluates to true, then returns hsh.

h = {:a => 100, :b => 200, :c => 300 }

h.delete_if {|k,v| v >= 200 }    #=> {:a => 100}


3262
3263
3264
3265
3266
# File 'lib/source/ruby.rb', line 3262

def delete_if
  `var c=this._contents`
  `for(var x in c){try{if(x.slice(1,2)=='_'&&$T(#{yield(`c[x][0]`,`c[x][1]`)})){delete(c[x]);};}catch(e){switch(e.__keyword__){case 'next':if($T(e._value)){delete(c[x]);};break;case 'break':return e._value;break;default:throw(e);};};}`
  return self
end

#eachObject

call-seq:

hsh.each { |key, value| block } -> hsh
hsh.each { |kv_array| block }   -> hsh

Calls block once for each key in hsh, passing the key and value to the block as a two-element array, or as separate key and value elements if the block has two formal parameters. See also Hash.each_pair, which will be marginally more efficient for blocks with two parameters.

h = {:a => 100, :b => 200}

h.each {|k,v| puts "key is #{k.inspect}, value is #{v.inspect}" }   #=> {:a => 100, :b => 200}
h.each {|kv|  puts "key-value array is #{kv.inspect}" }             #=> {:a => 100, :b => 200}

produces:

key is :a, value is 100
key is :b, value is 200
key-value array is [:a, 100]
key-value array is [:b, 100]


3289
3290
3291
3292
3293
# File 'lib/source/ruby.rb', line 3289

def each
  `var c=this._contents`
  `for(var x in c){try{if(x.slice(1,2)=='_'){var kv=c[x];_block._arity==1?#{yield(`[kv[0],kv[1]]`)}:#{yield(`kv[0],kv[1]`)}};}catch(e){switch(e.__keyword__){case 'next':;break;case 'break':return e._value;break;default:throw(e);};};}`
  return self
end

#each_keyObject

call-seq:

hsh.each_key { |key| block } -> hsh

Calls block once for each key in hsh, passing the key as a parameter.

h = {:a => 100, :b => 200}

h.each_key {|k| puts k.inspect }    #=> {:a => 100, :b => 200}

produces:

:a
:b


3309
3310
3311
3312
3313
# File 'lib/source/ruby.rb', line 3309

def each_key
  `var c=this._contents`
  `for(var x in c){try{if(x.slice(1,2)=='_'){#{yield `c[x][0]`}};}catch(e){switch(e.__keyword__){case 'next':;break;case 'break':return e._value;break;default:throw(e);};};}`
  return self
end

#each_pairObject

call-seq:

hsh.each_key { |key| block } -> hsh

Calls block once for each key in hsh, passing the key and value as parameters.

h = {:a => 100, :b => 200}

h.each_pair {|k,v| puts "#{k.inspect} is #{v.inspect}" }    #=> {:a => 100, :b => 200}

produces:

:a is 100
:b is 200


3330
3331
3332
3333
3334
# File 'lib/source/ruby.rb', line 3330

def each_pair
  `var c=this._contents`
  `for(var x in c){try{if(x.slice(1,2)=='_'){var kv=c[x];#{yield(`kv[0]`,`kv[1]`)}};}catch(e){switch(e.__keyword__){case 'next':;break;case 'break':return e._value;break;default:throw(e);};};}`
  return self
end

#each_valueObject

call-seq:

hsh.each_key { |key| block } -> hsh

Calls block once for each key in hsh, passing the value as a parameter.

h = {:a => 100, :b => 200}

h.each_value {|v| puts v.inspect }    #=> {:a => 100, :b => 200}

produces:

100
200


3351
3352
3353
3354
3355
# File 'lib/source/ruby.rb', line 3351

def each_value
  `var c=this._contents`
  `for(var x in c){try{if(x.slice(1,2)=='_'){#{yield `c[x][1]`}};}catch(e){switch(e.__keyword__){case 'next':;break;case 'break':return e._value;break;default:throw(e);};};}`
  return self
end

#empty?Boolean

call-seq:

hsh.empty? -> true or false

Returns true if hsh contains no key-value pairs.

{}.empty?   #=> true

Returns:

  • (Boolean)


3364
3365
3366
3367
# File 'lib/source/ruby.rb', line 3364

def empty?
  `for(var x in this._contents){if(x.slice(1,2)=='_'){return false;};}`
  return true
end

#fetch(key, &block) ⇒ Object

call-seq:

hsh.fetch(key [, default])     -> obj
hsh.fetch(key) { |key| block } -> obj

Returns a value from hsh for the given key. If the key is not found, returns the default object or evaluates block, or raises IndexError if neither are given.

h = {:a => 100, :b => 200}

h.fetch(:a)                                     #=> 100
h.fetch(:z, 'No value')                         #=> "No value"
h.fetch(:z) { |k| "No value at #{k.inspect}"}   #=> "No value at :z"


3383
3384
3385
3386
3387
# File 'lib/source/ruby.rb', line 3383

def fetch(key, &block)
  `var c=this._contents,k=key.m$hash(),kv=c[k]`
  `if(kv!=null){return kv[1];}`
  return `typeof(block)=='function'?block(key):block`
end

#has_key?(k) ⇒ Boolean

call-seq:

hsh.has_key?(key) -> true or false
hsh.include?(key) -> true or false
hsh.key?(key)     -> true or false
hsh.member?(key)  -> true or false

Returns true if the given key is present in hsh.

h = {:a => 100, :b => 200}

h.has_key?(:a)    #=> true
h.has_key?(:z)    #=> false

Returns:

  • (Boolean)


3402
3403
3404
# File 'lib/source/ruby.rb', line 3402

def has_key?(k)
  `!!this._contents[k.m$hash()]`
end

#has_value?(value) ⇒ Boolean

call-seq:

hsh.has_value?(value) -> true or false
hsh.value?(value)     -> true or false

Returns true if there is any key in hsh such that hsh[key] == value.

h = {:a => 100, :b => 200}

h.has_value?(100)   #=> true
h.has_value?(999)   #=> false

Returns:

  • (Boolean)


3418
3419
3420
3421
3422
# File 'lib/source/ruby.rb', line 3418

def has_value?(value)
  `var c=this._contents`
  `for(var x in c){if(x.slice(1,2)=='_'&&c[x][1].m$_eql2(value)){return true;};}`
  return false
end

#hashObject

:nodoc:



3424
3425
# File 'lib/source/ruby.rb', line 3424

def hash # :nodoc:
end

#include?(k) ⇒ Boolean

call-seq:

hsh.has_key?(key) -> true or false
hsh.include?(key) -> true or false
hsh.key?(key)     -> true or false
hsh.member?(key)  -> true or false

Returns true if the given key is present in hsh.

h = {:a => 100, :b => 200}

h.include?(:a)    #=> true
h.include?(:z)    #=> false

Returns:

  • (Boolean)


3440
3441
3442
# File 'lib/source/ruby.rb', line 3440

def include?(k)
  `!!this._contents[k.m$hash()]`
end

#index(value) ⇒ Object

call-seq:

hsh.index(value) -> key or nil

Returns the key for a given value. If not found, returns nil.

h = {:a => 100, :b => 200}

h.index(100)    #=> :a
h.index(999)    #=> nil


3454
3455
3456
3457
3458
# File 'lib/source/ruby.rb', line 3454

def index(value)
  `var c=this._contents`
  `for(var x in c){var kv=c[x];if(x.slice(1,2)=='_'&&kv[1].m$_eql2(value)){return kv[0];};}`
  return nil
end

#inspectObject

call-seq:

hsh.inspect -> string

Return the contents of hsh as a string.

h = Hash[:a,100,:b,200]

h.inspect   #=> "{:a => 100, :b => 200}"


3469
3470
3471
3472
3473
# File 'lib/source/ruby.rb', line 3469

def inspect
  `var contents=[],c=this._contents`
  `for(var x in c){if(x.slice(1,2)=='_'){var kv=c[x];contents.push(kv[0].m$inspect()._value+' => '+kv[1].m$inspect()._value);};}`
  return `$q('{'+contents.join(', ')+'}')`
end

#invertObject

call-seq:

hsh.invert -> hash

Returns a new hash created by using hsh’s values as keys, and its keys as values.

h = {:n => 100, :m => 100, :y => 300, :d => 200, :a => 0 }

h.invert    #=> {100 => :m, 300 => :y, 200 => :d, 0 => :a}


3485
3486
3487
3488
3489
# File 'lib/source/ruby.rb', line 3485

def invert
  `var c=this._contents,result=c$Hash.m$new()`
  `for(var x in c){if(x.slice(1,2)=='_'){var ckv=c[x],rkv=result._contents[ckv[1].m$hash()]=[];rkv[0]=ckv[1];rkv[1]=ckv[0]};}`
  return `result`
end

#key?(k) ⇒ Boolean

call-seq:

hsh.has_key?(key) -> true or false
hsh.include?(key) -> true or false
hsh.key?(key)     -> true or false
hsh.member?(key)  -> true or false

Returns true if the given key is present in hsh.

h = {:a => 100, :b => 200}

h.key?(:a)    #=> true
h.key?(:z)    #=> false

Returns:

  • (Boolean)


3504
3505
3506
# File 'lib/source/ruby.rb', line 3504

def key?(k)
  `!!this._contents[k.m$hash()]`
end

#keysObject

call-seq:

hsh.keys -> array

Returns a new array populated with the keys in hsh. See also Hash#values.

h = {:a => 100, :b => 200}

h.keys    #=> [:a, :b]


3518
3519
3520
3521
3522
# File 'lib/source/ruby.rb', line 3518

def keys
  `var c=this._contents,result=[]`
  `for(var x in c){if(x.slice(1,2)=='_'){result.push(c[x][0]);};}`
  return `result`
end

#lengthObject

call-seq:

hsh.length -> integer
hsh.size   -> integer

Returns the number of key-value pairs in hsh.

h = {:a => 100, :b => 200}

h.length    #=> 2
h.clear     #=> {}
h.length    #=> 0


3536
3537
3538
3539
3540
# File 'lib/source/ruby.rb', line 3536

def length
  `var c=this._contents,result=0`
  `for(var x in c){if(x.slice(1,2)=='_'){result++;};}`
  return `result`
end

#member?(k) ⇒ Boolean

call-seq:

hsh.has_key?(key) -> true or false
hsh.include?(key) -> true or false
hsh.key?(key)     -> true or false
hsh.member?(key)  -> true or false

Returns true if the given key is present in hsh.

h = {:a => 100, :b => 200}

h.member?(:a)   #=> true
h.member?(:z)   #=> false

Returns:

  • (Boolean)


3555
3556
3557
# File 'lib/source/ruby.rb', line 3555

def member?(k)
  `!!this._contents[k.m$hash()]`
end

#merge(other) ⇒ Object

call-seq:

hsh.merge(other)                                       -> hash
hsh.merge(other) { |key, old_value, new_value| block } -> hash

Returns a new hash containing the contents of other and the contents of hsh, using the value from other in the case of duplicate keys.

h1 = {:a => 100, :b => 200}
h2 = {:a => 150, :c => 300}

h1.merge(h2)                                #=> {:a => 150, :b => 200, :c => 300}
h1.merge(h2) {|k,oldv,newv| oldv * newv }   #=> {:a => 15000, :b => 200, :c => 300}
h1                                          #=> {:a => 100, :b => 200}

FIX: Doesn’t handle loop control keywords



3574
3575
3576
3577
3578
3579
# File 'lib/source/ruby.rb', line 3574

def merge(other)
  `var c=this._contents,o=other._contents,result=c$Hash.m$new(),r=result._contents`
  `for(var x in c){if(x.slice(1,2)=='_'){r[x]=c[x];};}`
  `for(var x in o){var ckv=c[x],okv=o[x];if(x.slice(1,2)=='_'){typeof(_block)=='function'&&ckv!=null?r[x]=[ckv[0],#{yield(`ckv[0]`,`ckv[1]`,`okv[1]`)}]:r[x]=okv;};}`
  return `result`
end

#merge!(other) ⇒ Object

call-seq:

hsh.merge!(other)                                       -> hash
hsh.merge!(other) { |key, old_value, new_value| block } -> hash
hsh.update(other)                                       -> hash
hsh.update(other) { |key, old_value, new_value| block } -> hash

Returns hsh with the contents of other added to it, overwriting duplicate entries in hsh with those from other.

h1 = {:a => 100, :b => 200}
h2 = {:a => 150, :c => 300}

h1.merge!(h2)                                 #=> {:a => 150, :b => 200, :c => 300}
h1.merge!(h2) {|k,oldv,newv| oldv * newv }    #=> {:a => 22500, :b => 200, :c => 90000}
h1                                            #=> {:a => 22500, :b => 200, :c => 90000}

FIX: Doesn’t handle loop control keywords



3598
3599
3600
3601
3602
# File 'lib/source/ruby.rb', line 3598

def merge!(other)
  `var c=this._contents,o=other._contents`
  `for(var x in o){var ckv=c[x],okv=o[x];if(x.slice(1,2)=='_'){typeof(_block)=='function'&&ckv!=null?ckv[1]=#{yield(`ckv[0]`,`ckv[1]`,`okv[1]`)}:c[x]=okv;};}`
  return self
end

#rejectObject

call-seq:

hsh.reject { |key, value| block } -> hash

Returns a new hash consisting of the key-value pairs for which block evaluates to nil or false.

h = {:a => 100, :b => 200, :c => 300}

h.reject {|k,v| v > 100 }   #=> {:a => 100}
h.reject {|k,v| v < 200 }   #=> {:b => 200, :c => 300}
h                           #=> {:a => 100, :b => 200, :c => 300}


3616
3617
3618
3619
3620
# File 'lib/source/ruby.rb', line 3616

def reject
  `var c=this._contents,result=c$Hash.m$new()`
  `for(var x in c){try{var kv=c[x];if(x.slice(1,2)=='_'&&!$T(#{yield(`kv[0]`,`kv[1]`)})){result._contents[x]=kv;};}catch(e){switch(e.__keyword__){case 'next':if(!$T(e._value)){result._contents[x]=kv;};break;case 'break':return e._value;break;default:throw(e);};};}`
  return `result`
end

#reject!Object

call-seq:

hsh.reject! { |key, value| block } -> hsh or nil

Removes key-value pairs for which block evaluates to nil or false and returns hsh, or nil if no changes were made.

h = {:a => 100, :b => 200, :c => 300}

h.reject! {|k,v| v > 100 }    #=> {:a => 100}
h.reject! {|k,v| v > 100 }    #=> nil
h                             #=> {:a => 100}


3635
3636
3637
3638
3639
# File 'lib/source/ruby.rb', line 3635

def reject!
  `var c=this._contents,u=true`
  `for(var x in c){try{var kv=c[x];if(x.slice(1,2)=='_'&&$T(#{yield(`kv[0]`,`kv[1]`)})){u=false;delete(c[x]);};}catch(e){switch(e.__keyword__){case 'next':if($T(e._value)){u=false;delete(c[x]);};break;case 'break':return e._value;break;default:throw(e);};};}`
  return `u?nil:this`
end

#replace(other) ⇒ Object

call-seq:

hsh.replace(other) -> hsh

Replaces the contents of hsh with the contents of other.

h = {:a => 100, :b => 200}

h.replace(:c => 300, :d => 400)   #=> {:c => 300, :d => 400}
h                                 #=> {:c => 300, :d => 400}


3651
3652
3653
3654
3655
3656
# File 'lib/source/ruby.rb', line 3651

def replace(other)
  `this._contents={}`
  `var c=this._contents,o=other._contents`
  `for(var x in o){if(x.slice(1,2)=='_'){c[x]=o[x];};}`
  return self
end

#selectObject

call-seq:

hsh.select { |key, value| block } -> array

Returns an array consisting of [key,value] pairs for which the block returns true. Also see Hash.values_at.

h = {:a => 100, :b => 200, :c => 300}

h.select {|k,v| v > 100 }   #=> [[:b, 200], [:c, 300]]
h.select {|k,v| v < 200}    #=> [[:a, 100]]


3669
3670
3671
3672
3673
# File 'lib/source/ruby.rb', line 3669

def select
  `var c=this._contents,result=[]`
  `for(var x in c){try{var kv=c[x];if(x.slice(1,2)=='_'&&$T(#{yield(`kv[0]`,`kv[1]`)})){result.push(kv);};}catch(e){switch(e.__keyword__){case 'next':if($T(e._value)){result.push(kv);};break;case 'break':return e._value;break;default:throw(e);};};}`
  return `result`
end

#shiftObject

call-seq:

hsh.shift -> array or object

Removes a key-value pair from hsh and returns it as the two-item array [key, value], or returns the default value if hsh is empty.

h = {:a => 100, :b => 200}

h.shift   #=> [:a, 100]
h.shift   #=> [:b, 200]
h.shift   #=> nil
h         #=> {}


3688
3689
3690
3691
3692
# File 'lib/source/ruby.rb', line 3688

def shift
  `var c=this._contents,d=this._default,result=typeof(d)=='function'?d(nil):d`
  `for(var x in c){if(x.slice(1,2)=='_'){result=[c[x][0],c[x][1]];delete(c[x]);break;};}`
  return `result`
end

#sizeObject

call-seq:

hsh.length -> integer
hsh.size   -> integer

Returns the number of key-value pairs in hsh.

h = {:a => 100, :b => 200}

h.size    #=> 2
h.clear   #=> {}
h.size    #=> 0


3706
3707
3708
3709
3710
# File 'lib/source/ruby.rb', line 3706

def size
  `var c=this._contents,result=0`
  `for(var x in c){if(x.slice(1,2)=='_'){result++;};}`
  return `result`
end

#sort(&block) ⇒ Object

call-seq:

hsh.sort                 -> array
hsh.sort { |a,b| block } -> array

Converts hsh to a nested array of [key, value] arrays and sorts it, using Array#sort.

h = {3 => 'a', 1 => 'b', 2 => 'c'}

h.sort                          #=> [[1, "b"], [2, "c"], [3, "a"]]
h.sort {|a,b| a[1] <=> b[1] }   #=> [[3, "a"], [1, "b"], [2, "c"]]

FIX: Doesn’t handle loop control keywords



3725
3726
3727
3728
3729
# File 'lib/source/ruby.rb', line 3725

def sort(&block)
  `var c=this._contents,result=[]`
  `for(var x in c){if(x.slice(1,2)=='_'){result.push(c[x]);};}`
  return `c$Array.prototype._quickSort.call(result,0,result.length,block)`
end

#store(k, v) ⇒ Object

call-seq:

hsh[key] = value     -> value
hsh.store(key,value) -> value

Element Assignment – associates the value given by value with the key given by key. The object key should not have its value changed while it is in use as a key.

h = {:a => 100, :b => 200}

h.store(:a,150)   #=> 150
h.store(:c,300)   #=> 300
h                 #=> {:a => 150, :b => 200, :c => 300}


3745
3746
3747
3748
# File 'lib/source/ruby.rb', line 3745

def store(k,v)
  `this._contents[k.m$hash()]=[k,v]`
  return `v`
end

#to_aObject

call-seq:

hsh.to_a -> array

Converts hash to a nested array of [key, value] arrays.

h = {:a => 100, :b => 200}

h.to_a    #=> [[:a, 100], [:b, 200]]


3759
3760
3761
3762
3763
# File 'lib/source/ruby.rb', line 3759

def to_a
  `var c=this._contents,result=[]`
  `for(var x in c){if(x.slice(1,2)=='_'){result.push(c[x]);};}`
  return `result`
end

#to_hashObject

call-seq:

hsh.to_hash -> hsh

Returns hsh.



3770
3771
3772
# File 'lib/source/ruby.rb', line 3770

def to_hash
  self
end

#to_sObject

call-seq:

hsh.to_s -> string

Converts hsh to a string by converting the hash to an array of [key, value] pairs and then converting that array to a string using Array#join with the default separator.



3781
3782
3783
3784
3785
# File 'lib/source/ruby.rb', line 3781

def to_s
  `var c=this._contents,result=[]`
  `for(var x in c){if(x.slice(1,2)=='_'){result.push(c[x]);};}`
  return `c$Array.prototype.m$join.call(result)`
end

#update(other) ⇒ Object

call-seq:

hsh.merge!(other)                                       -> hash
hsh.merge!(other) { |key, old_value, new_value| block } -> hash
hsh.update(other)                                       -> hash
hsh.update(other) { |key, old_value, new_value| block } -> hash

Returns hsh with the contents of other added to it, overwriting duplicate entries in hsh with those from other.

h1 = {:a => 100, :b => 200}
h2 = {:a => 150, :c => 300}

h1.update(h2)                                 #=> {:a => 150, :b => 200, :c => 300}
h1.update(h2) {|k,oldv,newv| oldv * newv }    #=> {:a => 22500, :b => 200, :c => 90000}
h1                                            #=> {:a => 22500, :b => 200, :c => 90000}

FIX: Doesn’t handle loop control keywords



3804
3805
3806
3807
3808
# File 'lib/source/ruby.rb', line 3804

def update(other)
  `var c=this._contents,o=other._contents`
  `for(var x in o){var ckv=c[x],okv=o[x];if(x.slice(1,2)=='_'){typeof(_block)=='function'&&ckv!=null?ckv[1]=#{yield(`ckv[0]`,`ckv[1]`,`okv[1]`)}:c[x]=okv;};}`
  return self
end

#value?(value) ⇒ Boolean

call-seq:

hsh.has_value?(value) -> true or false
hsh.value?(value)     -> true or false

Returns true if there is any key in hsh such that hsh[key] == value.

h = {:a => 100, :b => 200}

h.value?(100)   #=> true
h.value?(999)   #=> false

Returns:

  • (Boolean)


3822
3823
3824
3825
3826
# File 'lib/source/ruby.rb', line 3822

def value?(value)
  `var c=this._contents`
  `for(var x in this._contents){if(x.slice(1,2)=='_'&&c[x][1].m$_eql2(value)){return true;};}`
  return false
end

#valuesObject

call-seq:

hsh.values -> array

Returns a new array populated with the values of hsh. See also Hash#keys.

h = {:a => 100, :b => 200}

h.values    #=> [100, 200]


3838
3839
3840
3841
3842
# File 'lib/source/ruby.rb', line 3838

def values
  `var c=this._contents,result=[]`
  `for(var x in c){if(x.slice(1,2)=='_'){result.push(c[x][1]);};}`
  return `result`
end

#values_at(*args) ⇒ Object

call-seq:

hsh.values_at(key, ...) -> array

Returns an array containing the values associated with the given keys. See also Hash.select.

h = {:a => 100, :b => 200, :c => 300}

h.values_at(:a,:c)    #=> [100,300]


3854
3855
3856
3857
# File 'lib/source/ruby.rb', line 3854

def values_at(*args)
  `for(var i=0,l=args.length,c=this._contents,d=this._default,result=[];i<l;++i){var h=args[i].m$hash(),kv=c[h];result.push(kv?kv[1]:(typeof(d)=='function'?d(this,args[i]):d))}`
  return `result`
end