Class: Hash
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
-
.[](*args) ⇒ Object
call-seq: Hash[ [key (, | =>) value]* ] -> hash.
Instance Method Summary collapse
-
#==(other) ⇒ Object
call-seq: hsh == other -> true or false.
-
#[](k) ⇒ Object
call-seq: hsh -> value.
-
#[]=(k, v) ⇒ Object
call-seq: hsh = value -> value hsh.store(key,value) -> value.
-
#clear ⇒ Object
call-seq: hsh.clear -> hsh.
-
#default(key = nil) ⇒ Object
call-seq: hsh.default(key = nil) -> obj.
-
#default=(obj) ⇒ Object
call-seq: hsh.default = obj -> hsh.
-
#default_proc ⇒ Object
call-seq: hsh.default_proc -> proc.
-
#delete(k) ⇒ Object
call-seq: hsh.delete(key) -> value hsh.delete(key) { |key| block } -> value.
-
#delete_if ⇒ Object
call-seq: hsh.delete_if { |key, value| block } -> hsh.
-
#each ⇒ Object
call-seq: hsh.each { |key, value| block } -> hsh hsh.each { |kv_array| block } -> hsh.
-
#each_key ⇒ Object
call-seq: hsh.each_key { |key| block } -> hsh.
-
#each_pair ⇒ Object
call-seq: hsh.each_key { |key| block } -> hsh.
-
#each_value ⇒ Object
call-seq: hsh.each_key { |key| block } -> hsh.
-
#empty? ⇒ Boolean
call-seq: hsh.empty? -> true or false.
-
#fetch(key, &block) ⇒ Object
call-seq: hsh.fetch(key [, default]) -> obj hsh.fetch(key) { |key| block } -> obj.
-
#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.
-
#has_value?(value) ⇒ Boolean
call-seq: hsh.has_value?(value) -> true or false hsh.value?(value) -> true or false.
-
#hash ⇒ Object
:nodoc:.
-
#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.
-
#index(value) ⇒ Object
call-seq: hsh.index(value) -> key or nil.
-
#initialize(&block) ⇒ Hash
constructor
call-seq: Hash.new -> hash Hash.new(obj) -> hash Hash.new { |hash, key| block } -> hash.
-
#inspect ⇒ Object
call-seq: hsh.inspect -> string.
-
#invert ⇒ Object
call-seq: hsh.invert -> hash.
-
#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.
-
#keys ⇒ Object
call-seq: hsh.keys -> array.
-
#length ⇒ Object
call-seq: hsh.length -> integer hsh.size -> integer.
-
#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.
-
#merge(other) ⇒ Object
call-seq: hsh.merge(other) -> hash hsh.merge(other) { |key, old_value, new_value| block } -> hash.
-
#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.
-
#reject ⇒ Object
call-seq: hsh.reject { |key, value| block } -> hash.
-
#reject! ⇒ Object
call-seq: hsh.reject! { |key, value| block } -> hsh or nil.
-
#replace(other) ⇒ Object
call-seq: hsh.replace(other) -> hsh.
-
#select ⇒ Object
call-seq: hsh.select { |key, value| block } -> array.
-
#shift ⇒ Object
call-seq: hsh.shift -> array or object.
-
#size ⇒ Object
call-seq: hsh.length -> integer hsh.size -> integer.
-
#sort(&block) ⇒ Object
call-seq: hsh.sort -> array hsh.sort { |a,b| block } -> array.
-
#store(k, v) ⇒ Object
call-seq: hsh = value -> value hsh.store(key,value) -> value.
-
#to_a ⇒ Object
call-seq: hsh.to_a -> array.
-
#to_hash ⇒ Object
call-seq: hsh.to_hash -> hsh.
-
#to_s ⇒ Object
call-seq: hsh.to_s -> string.
-
#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.
-
#value?(value) ⇒ Boolean
call-seq: hsh.has_value?(value) -> true or false hsh.value?(value) -> true or false.
-
#values ⇒ Object
call-seq: hsh.values -> array.
-
#values_at(*args) ⇒ Object
call-seq: hsh.values_at(key, …) -> array.
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]
3077 3078 3079 3080 |
# File 'lib/source/ruby.rb', line 3077 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}
3037 3038 3039 3040 3041 |
# File 'lib/source/ruby.rb', line 3037 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
3097 3098 3099 3100 3101 3102 |
# File 'lib/source/ruby.rb', line 3097 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
3116 3117 3118 3119 3120 |
# File 'lib/source/ruby.rb', line 3116 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}
3136 3137 3138 3139 |
# File 'lib/source/ruby.rb', line 3136 def []=(k,v) `this._contents[k.m$hash()]=[k,v]` return `v` end |
#clear ⇒ Object
call-seq:
hsh.clear -> hsh
Removes all key-value pairs from hsh.
h = {:a => 1, :b => 2}
h.clear #=> {}
h #=> {}
3151 3152 3153 3154 |
# File 'lib/source/ruby.rb', line 3151 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
3175 3176 3177 3178 |
# File 'lib/source/ruby.rb', line 3175 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>
3199 3200 3201 3202 |
# File 'lib/source/ruby.rb', line 3199 def default=(obj) `this._default=obj` return self end |
#default_proc ⇒ Object
3216 3217 3218 3219 |
# File 'lib/source/ruby.rb', line 3216 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"
3236 3237 3238 3239 3240 |
# File 'lib/source/ruby.rb', line 3236 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_if ⇒ Object
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}
3252 3253 3254 3255 3256 |
# File 'lib/source/ruby.rb', line 3252 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 |
#each ⇒ Object
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]
3279 3280 3281 3282 3283 |
# File 'lib/source/ruby.rb', line 3279 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_key ⇒ Object
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
3299 3300 3301 3302 3303 |
# File 'lib/source/ruby.rb', line 3299 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_pair ⇒ Object
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
3320 3321 3322 3323 3324 |
# File 'lib/source/ruby.rb', line 3320 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_value ⇒ Object
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
3341 3342 3343 3344 3345 |
# File 'lib/source/ruby.rb', line 3341 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
3354 3355 3356 3357 |
# File 'lib/source/ruby.rb', line 3354 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"
3373 3374 3375 3376 3377 |
# File 'lib/source/ruby.rb', line 3373 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
3392 3393 3394 |
# File 'lib/source/ruby.rb', line 3392 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
3408 3409 3410 3411 3412 |
# File 'lib/source/ruby.rb', line 3408 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 |
#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
3430 3431 3432 |
# File 'lib/source/ruby.rb', line 3430 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
3444 3445 3446 3447 3448 |
# File 'lib/source/ruby.rb', line 3444 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 |
#inspect ⇒ Object
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}"
3459 3460 3461 3462 3463 |
# File 'lib/source/ruby.rb', line 3459 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 |
#invert ⇒ Object
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}
3475 3476 3477 3478 3479 |
# File 'lib/source/ruby.rb', line 3475 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
3494 3495 3496 |
# File 'lib/source/ruby.rb', line 3494 def key?(k) `!!this._contents[k.m$hash()]` end |
#keys ⇒ Object
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]
3508 3509 3510 3511 3512 |
# File 'lib/source/ruby.rb', line 3508 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 |
#length ⇒ Object
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
3526 3527 3528 3529 3530 |
# File 'lib/source/ruby.rb', line 3526 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
3545 3546 3547 |
# File 'lib/source/ruby.rb', line 3545 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
3564 3565 3566 3567 3568 3569 |
# File 'lib/source/ruby.rb', line 3564 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
3588 3589 3590 3591 3592 |
# File 'lib/source/ruby.rb', line 3588 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 |
#reject ⇒ Object
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}
3606 3607 3608 3609 3610 |
# File 'lib/source/ruby.rb', line 3606 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}
3625 3626 3627 3628 3629 |
# File 'lib/source/ruby.rb', line 3625 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}
3641 3642 3643 3644 3645 3646 |
# File 'lib/source/ruby.rb', line 3641 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 |
#select ⇒ Object
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]]
3659 3660 3661 3662 3663 |
# File 'lib/source/ruby.rb', line 3659 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 |
#shift ⇒ Object
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 #=> {}
3678 3679 3680 3681 3682 |
# File 'lib/source/ruby.rb', line 3678 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 |
#size ⇒ Object
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
3696 3697 3698 3699 3700 |
# File 'lib/source/ruby.rb', line 3696 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
3715 3716 3717 3718 3719 |
# File 'lib/source/ruby.rb', line 3715 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}
3735 3736 3737 3738 |
# File 'lib/source/ruby.rb', line 3735 def store(k,v) `this._contents[k.m$hash()]=[k,v]` return `v` end |
#to_a ⇒ Object
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]]
3749 3750 3751 3752 3753 |
# File 'lib/source/ruby.rb', line 3749 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_hash ⇒ Object
call-seq:
hsh.to_hash -> hsh
Returns hsh.
3760 3761 3762 |
# File 'lib/source/ruby.rb', line 3760 def to_hash self end |
#to_s ⇒ Object
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.
3771 3772 3773 3774 3775 |
# File 'lib/source/ruby.rb', line 3771 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
3794 3795 3796 3797 3798 |
# File 'lib/source/ruby.rb', line 3794 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
3812 3813 3814 3815 3816 |
# File 'lib/source/ruby.rb', line 3812 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 |
#values ⇒ Object
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]
3828 3829 3830 3831 3832 |
# File 'lib/source/ruby.rb', line 3828 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]
3844 3845 3846 3847 |
# File 'lib/source/ruby.rb', line 3844 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 |