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]
3064 3065 3066 3067 |
# File 'lib/source/ruby.rb', line 3064 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}
3024 3025 3026 3027 3028 |
# File 'lib/source/ruby.rb', line 3024 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
3084 3085 3086 3087 3088 3089 |
# File 'lib/source/ruby.rb', line 3084 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
3103 3104 3105 3106 3107 |
# File 'lib/source/ruby.rb', line 3103 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}
3123 3124 3125 3126 |
# File 'lib/source/ruby.rb', line 3123 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 #=> {}
3138 3139 3140 3141 |
# File 'lib/source/ruby.rb', line 3138 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
3162 3163 3164 3165 |
# File 'lib/source/ruby.rb', line 3162 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>
3186 3187 3188 3189 |
# File 'lib/source/ruby.rb', line 3186 def default=(obj) `this._default=obj` return self end |
#default_proc ⇒ Object
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]
3203 3204 3205 3206 |
# File 'lib/source/ruby.rb', line 3203 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"
3223 3224 3225 3226 3227 |
# File 'lib/source/ruby.rb', line 3223 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}
3239 3240 3241 3242 3243 |
# File 'lib/source/ruby.rb', line 3239 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._name){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]
3266 3267 3268 3269 3270 |
# File 'lib/source/ruby.rb', line 3266 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._name){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
3286 3287 3288 3289 3290 |
# File 'lib/source/ruby.rb', line 3286 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._name){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
3307 3308 3309 3310 3311 |
# File 'lib/source/ruby.rb', line 3307 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._name){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
3328 3329 3330 3331 3332 |
# File 'lib/source/ruby.rb', line 3328 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._name){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
3341 3342 3343 3344 |
# File 'lib/source/ruby.rb', line 3341 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"
3360 3361 3362 3363 3364 |
# File 'lib/source/ruby.rb', line 3360 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
3379 3380 3381 |
# File 'lib/source/ruby.rb', line 3379 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
3395 3396 3397 3398 3399 |
# File 'lib/source/ruby.rb', line 3395 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
3417 3418 3419 |
# File 'lib/source/ruby.rb', line 3417 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
3431 3432 3433 3434 3435 |
# File 'lib/source/ruby.rb', line 3431 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}"
3446 3447 3448 3449 3450 |
# File 'lib/source/ruby.rb', line 3446 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}
3462 3463 3464 3465 3466 |
# File 'lib/source/ruby.rb', line 3462 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
3481 3482 3483 |
# File 'lib/source/ruby.rb', line 3481 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]
3495 3496 3497 3498 3499 |
# File 'lib/source/ruby.rb', line 3495 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
3513 3514 3515 3516 3517 |
# File 'lib/source/ruby.rb', line 3513 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
3532 3533 3534 |
# File 'lib/source/ruby.rb', line 3532 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
3551 3552 3553 3554 3555 3556 |
# File 'lib/source/ruby.rb', line 3551 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
3575 3576 3577 3578 3579 |
# File 'lib/source/ruby.rb', line 3575 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}
3593 3594 3595 3596 3597 |
# File 'lib/source/ruby.rb', line 3593 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._name){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}
3612 3613 3614 3615 3616 |
# File 'lib/source/ruby.rb', line 3612 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._name){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}
3628 3629 3630 3631 3632 3633 |
# File 'lib/source/ruby.rb', line 3628 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]]
3646 3647 3648 3649 3650 |
# File 'lib/source/ruby.rb', line 3646 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._name){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 #=> {}
3665 3666 3667 3668 3669 |
# File 'lib/source/ruby.rb', line 3665 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
3683 3684 3685 3686 3687 |
# File 'lib/source/ruby.rb', line 3683 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
3702 3703 3704 3705 3706 |
# File 'lib/source/ruby.rb', line 3702 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}
3722 3723 3724 3725 |
# File 'lib/source/ruby.rb', line 3722 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]]
3736 3737 3738 3739 3740 |
# File 'lib/source/ruby.rb', line 3736 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.
3747 3748 3749 |
# File 'lib/source/ruby.rb', line 3747 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.
3758 3759 3760 3761 3762 |
# File 'lib/source/ruby.rb', line 3758 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
3781 3782 3783 3784 3785 |
# File 'lib/source/ruby.rb', line 3781 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
3799 3800 3801 3802 3803 |
# File 'lib/source/ruby.rb', line 3799 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]
3815 3816 3817 3818 3819 |
# File 'lib/source/ruby.rb', line 3815 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]
3831 3832 3833 3834 |
# File 'lib/source/ruby.rb', line 3831 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 |