Class: Hash
- Defined in:
- lib/source/ruby.rb,
lib/source/redshift/request.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
-
.[](*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(*args, &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_query_string(base = '') ⇒ Object
call-seq: hsh.to_query_string -> string.
-
#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(*args, &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]
3113 3114 3115 3116 |
# File 'lib/source/ruby.rb', line 3113 def initialize(*args,&block) `this.__default__=(args[0]==null?block==null?nil:block:args[0])` `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}
3073 3074 3075 3076 3077 |
# File 'lib/source/ruby.rb', line 3073 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
3133 3134 3135 3136 3137 3138 |
# File 'lib/source/ruby.rb', line 3133 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
3152 3153 3154 3155 3156 |
# File 'lib/source/ruby.rb', line 3152 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}
3172 3173 3174 3175 |
# File 'lib/source/ruby.rb', line 3172 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 #=> {}
3187 3188 3189 3190 |
# File 'lib/source/ruby.rb', line 3187 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
3211 3212 3213 3214 |
# File 'lib/source/ruby.rb', line 3211 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>
3235 3236 3237 3238 |
# File 'lib/source/ruby.rb', line 3235 def default=(obj) `this.__default__=obj` return self end |
#default_proc ⇒ Object
3252 3253 3254 3255 |
# File 'lib/source/ruby.rb', line 3252 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"
3272 3273 3274 3275 3276 |
# File 'lib/source/ruby.rb', line 3272 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}
3288 3289 3290 3291 3292 |
# File 'lib/source/ruby.rb', line 3288 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.__return__)){delete(c[x]);};break;case 'break':return e.__return__;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]
3315 3316 3317 3318 3319 |
# File 'lib/source/ruby.rb', line 3315 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.__return__;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
3335 3336 3337 3338 3339 |
# File 'lib/source/ruby.rb', line 3335 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.__return__;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
3356 3357 3358 3359 3360 |
# File 'lib/source/ruby.rb', line 3356 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.__return__;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
3377 3378 3379 3380 3381 |
# File 'lib/source/ruby.rb', line 3377 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.__return__;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
3390 3391 3392 3393 |
# File 'lib/source/ruby.rb', line 3390 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"
3409 3410 3411 3412 3413 |
# File 'lib/source/ruby.rb', line 3409 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
3428 3429 3430 |
# File 'lib/source/ruby.rb', line 3428 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
3444 3445 3446 3447 3448 |
# File 'lib/source/ruby.rb', line 3444 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
3466 3467 3468 |
# File 'lib/source/ruby.rb', line 3466 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
3480 3481 3482 3483 3484 |
# File 'lib/source/ruby.rb', line 3480 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}"
3495 3496 3497 3498 3499 |
# File 'lib/source/ruby.rb', line 3495 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}
3511 3512 3513 3514 3515 |
# File 'lib/source/ruby.rb', line 3511 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
3530 3531 3532 |
# File 'lib/source/ruby.rb', line 3530 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]
3544 3545 3546 3547 3548 |
# File 'lib/source/ruby.rb', line 3544 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
3562 3563 3564 3565 3566 |
# File 'lib/source/ruby.rb', line 3562 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
3581 3582 3583 |
# File 'lib/source/ruby.rb', line 3581 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
3600 3601 3602 3603 3604 3605 |
# File 'lib/source/ruby.rb', line 3600 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
3624 3625 3626 3627 3628 |
# File 'lib/source/ruby.rb', line 3624 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}
3642 3643 3644 3645 3646 |
# File 'lib/source/ruby.rb', line 3642 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.__return__)){result.__contents__[x]=kv;};break;case 'break':return e.__return__;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}
3661 3662 3663 3664 3665 |
# File 'lib/source/ruby.rb', line 3661 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.__return__)){u=false;delete(c[x]);};break;case 'break':return e.__return__;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}
3677 3678 3679 3680 3681 3682 |
# File 'lib/source/ruby.rb', line 3677 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]]
3695 3696 3697 3698 3699 |
# File 'lib/source/ruby.rb', line 3695 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.__return__)){result.push(kv);};break;case 'break':return e.__return__;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 #=> {}
3714 3715 3716 3717 3718 |
# File 'lib/source/ruby.rb', line 3714 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
3732 3733 3734 3735 3736 |
# File 'lib/source/ruby.rb', line 3732 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
3751 3752 3753 3754 3755 |
# File 'lib/source/ruby.rb', line 3751 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._quick_sort.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}
3771 3772 3773 3774 |
# File 'lib/source/ruby.rb', line 3771 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]]
3785 3786 3787 3788 3789 |
# File 'lib/source/ruby.rb', line 3785 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.
3796 3797 3798 |
# File 'lib/source/ruby.rb', line 3796 def to_hash self end |
#to_query_string(base = '') ⇒ Object
call-seq:
hsh.to_query_string -> string
Returns a string representing hsh formatted as HTTP data.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/source/redshift/request.rb', line 236 def to_query_string(base = '') query_string = [] self.each do |k,v| next if v.nil? k = base.empty? ? k.to_s : "%s[%s]" % [base,k] case v when Hash result = v.to_query_string(k) when Array qs = {} `for(var i=0,l=v.length;i<l;i++){#{qs[i] = v[i]}}` #v.each_with_index do |v,i| # qs[i] = v #end result = qs.to_query_string(k) else result = "%s=%s" % [k, `$q(encodeURIComponent(v))`] end query_string.push(result) end return query_string.join('&') 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.
3807 3808 3809 3810 3811 |
# File 'lib/source/ruby.rb', line 3807 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
3830 3831 3832 3833 3834 |
# File 'lib/source/ruby.rb', line 3830 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
3848 3849 3850 3851 3852 |
# File 'lib/source/ruby.rb', line 3848 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]
3864 3865 3866 3867 3868 |
# File 'lib/source/ruby.rb', line 3864 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]
3880 3881 3882 3883 |
# File 'lib/source/ruby.rb', line 3880 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 |