249
250
rb_hash_modify_check(VALUE hash)
251
if (OBJ_FROZEN(hash)) rb_error_frozen("hash");
252
rb_check_frozen(hash);
252
253
if (!OBJ_UNTRUSTED(hash) && rb_safe_level() >= 4)
253
254
rb_raise(rb_eSecurityError, "Insecure: can't modify hash");
418
419
return rb_convert_type(hash, T_HASH, "Hash", "to_hash");
423
rb_check_hash_type(VALUE hash)
425
return rb_check_convert_type(hash, T_HASH, "Hash", "to_hash");
423
430
* Hash.try_convert(obj) -> hash or nil
433
440
rb_hash_s_try_convert(VALUE dummy, VALUE hash)
435
return rb_check_convert_type(hash, T_HASH, "Hash", "to_hash");
442
return rb_check_hash_type(hash);
501
508
rb_hash_aref(VALUE hash, VALUE key)
505
512
if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
506
return rb_funcall(hash, id_default, 1, key);
513
if (!FL_TEST(hash, HASH_PROC_DEFAULT) &&
514
rb_method_basic_definition_p(CLASS_OF(hash), id_default)) {
515
return RHASH_IFNONE(hash);
518
return rb_funcall(hash, id_default, 1, key);
512
525
rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
516
529
if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
517
530
return def; /* without Hash#default */
558
571
rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash)
560
573
VALUE key, if_none;
562
575
long block_given;
564
577
rb_scan_args(argc, argv, "11", &key, &if_none);
571
584
if (block_given) return rb_yield(key);
573
586
volatile VALUE desc = rb_protect(rb_inspect, key, 0);
574
if (NIL_P(desc) || RSTRING_LEN(desc) > 65) {
575
588
desc = rb_any_to_s(key);
590
desc = rb_str_ellipsize(desc, 65);
577
591
rb_raise(rb_eKeyError, "key not found: %s", RSTRING_PTR(desc));
726
738
* hsh.key(value) -> key
728
* Returns the key for a given value. If not found, returns <code>nil</code>.
740
* Returns the key of an occurrence of a given value. If the value is
741
* not found, returns <code>nil</code>.
730
* h = { "a" => 100, "b" => 200 }
743
* h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
731
744
* h.key(200) #=> "b"
732
746
* h.key(999) #=> nil
932
946
* hsh.reject {| key, value | block } -> a_hash
947
* hsh.reject -> an_enumerator
934
949
* Same as <code>Hash#delete_if</code>, but works on (and returns) a
935
950
* copy of the <i>hsh</i>. Equivalent to
1115
1136
st_insert(RHASH(hash)->ntbl, key, val);
1118
st_insert2(RHASH(hash)->ntbl, key, val, rb_str_new4);
1139
st_insert2(RHASH(hash)->ntbl, key, val, copy_str_key);
1349
1370
if (key == Qundef) return ST_CONTINUE;
1371
str2 = rb_inspect(key);
1350
1372
if (RSTRING_LEN(str) > 1) {
1351
1373
rb_str_cat2(str, ", ");
1353
str2 = rb_inspect(key);
1376
rb_enc_copy(str, str2);
1354
1378
rb_str_buf_append(str, str2);
1355
1379
OBJ_INFECT(str, str2);
1356
1380
rb_str_buf_cat2(str, "=>");
1542
1566
eql_i(VALUE key, VALUE val1, VALUE arg)
1544
1568
struct equal_data *data = (struct equal_data *)arg;
1547
1571
if (key == Qundef) return ST_CONTINUE;
1548
1572
if (!st_lookup(data->tbl, key, &val2)) {
1549
1573
data->result = Qfalse;
1550
1574
return ST_STOP;
1552
if (!(data->eql ? rb_eql(val1, val2) : (int)rb_equal(val1, val2))) {
1576
if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) {
1553
1577
data->result = Qfalse;
1554
1578
return ST_STOP;
1644
1668
hash_i(VALUE key, VALUE val, VALUE arg)
1646
1670
st_index_t *hval = (st_index_t *)arg;
1671
st_index_t hdata[2];
1648
1673
if (key == Qundef) return ST_CONTINUE;
1649
*hval ^= rb_hash_end(rb_hash_uint(rb_hash_start(rb_hash(key)), rb_hash(val)));
1674
hdata[0] = rb_hash(key);
1675
hdata[1] = rb_hash(val);
1676
*hval ^= st_hash(hdata, sizeof(hdata), 0);
1650
1677
return ST_CONTINUE;
1658
1685
if (!RHASH(hash)->ntbl)
1659
1686
return LONG2FIX(0);
1660
1687
hval = RHASH(hash)->ntbl->num_entries;
1688
if (!hval) return LONG2FIX(0);
1662
hval = rb_hash_end(rb_hash_uint(rb_hash_start(rb_hash(rb_cHash)), hval));
1690
hval = rb_hash_uint(rb_hash_start(rb_hash(rb_cHash)), hval);
1664
1692
rb_hash_foreach(hash, hash_i, (VALUE)&hval);
1693
hval = rb_hash_end(hval);
1665
1694
return INT2FIX(hval);
1800
rb_hash_update_func *func;
1804
rb_hash_update_func_i(VALUE key, VALUE value, VALUE arg0)
1806
struct update_arg *arg = (struct update_arg *)arg0;
1807
VALUE hash = arg->hash;
1809
if (key == Qundef) return ST_CONTINUE;
1810
if (rb_hash_has_key(hash, key)) {
1811
value = (*arg->func)(key, rb_hash_aref(hash, key), value);
1813
hash_update(hash, key);
1814
st_insert(RHASH(hash)->ntbl, key, value);
1819
rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func)
1821
rb_hash_modify(hash1);
1822
hash2 = to_hash(hash2);
1824
struct update_arg arg;
1827
rb_hash_foreach(hash2, rb_hash_update_func_i, (VALUE)&arg);
1830
rb_hash_foreach(hash2, rb_hash_update_i, hash1);
1771
1837
* hsh.merge(other_hash) -> new_hash
1848
* hash.rassoc(key) -> an_array or nil
1914
* hash.rassoc(obj) -> an_array or nil
1850
1916
* Searches through the hash comparing _obj_ with the value using <code>==</code>.
1851
1917
* Returns the first key-value pair (two-element array) that matches. See
1948
2014
static char **origenviron;
1950
#define GET_ENVIRON(e) (e = rb_w32_get_environ())
2016
#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
1951
2017
#define FREE_ENVIRON(e) rb_w32_free_environ(e)
1952
2018
static char **my_environ;
1963
2029
#define FREE_ENVIRON(e)
1965
2031
#ifdef ENV_IGNORECASE
1966
#define ENVMATCH(s1, s2) (STRCASECMP(s1, s2) == 0)
1967
#define ENVNMATCH(s1, s2, n) (STRNCASECMP(s1, s2, n) == 0)
2032
#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2033
#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
1969
#define ENVMATCH(n1, n2) (strcmp(n1, n2) == 0)
1970
#define ENVNMATCH(s1, s2, n) (memcmp(s1, s2, n) == 0)
2035
#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2036
#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
2081
* ENV.delete(name) -> value
2082
* ENV.delete(name) { |name| } -> value
2084
* Deletes the environment variable with +name+ and returns the value of the
2085
* variable. If a block is given it will be called when the named environment
2014
2089
env_delete_m(VALUE obj, VALUE name)
2023
2098
static int env_path_tainted(const char *);
2102
* ENV[name] -> value
2104
* Retrieves the +value+ for environment variable +name+ as a String. Returns
2105
* +nil+ if the named variable does not exist.
2026
2108
rb_f_getenv(VALUE obj, VALUE name)
2132
* :yield: missing_name
2134
* ENV.fetch(name) -> value
2135
* ENV.fetch(name, default) -> value
2136
* ENV.fetch(name) { |missing_name| ... } -> value
2138
* Retrieves the environment variable +name+.
2140
* If the given name does not exist and neither +default+ nor a block a
2141
* provided an IndexError is raised. If a block is given it is called with
2142
* the missing name to provide a value. If a default value is given it will
2143
* be returned when no block is given.
2050
2146
env_fetch(int argc, VALUE *argv)
2230
getenvsize(const char* p)
2232
const char* porg = p;
2233
while (*p++) p += strlen(p) + 1;
2234
return p - porg + 1;
2239
return (rb_w32_osver() >= 5) ? 32767 : 5120;
2133
2244
ruby_setenv(const char *name, const char *value)
2135
2246
#if defined(_WIN32)
2138
2248
int failed = 0;
2139
2249
if (strchr(name, '=')) {
2140
2251
errno = EINVAL;
2141
2252
rb_sys_fail("ruby_setenv");
2144
len = strlen(name) + 1 + strlen(value) + 1;
2145
buf = ALLOCA_N(char, len);
2146
snprintf(buf, len, "%s=%s", name, value);
2147
failed = putenv(buf);
2255
const char* p = GetEnvironmentStringsA();
2256
if (!p) goto fail; /* never happen */
2257
if (strlen(name) + 2 + strlen(value) + getenvsize(p) >= getenvblocksize()) {
2258
goto fail; /* 2 for '=' & '\0' */
2260
buf = rb_sprintf("%s=%s", name, value);
2263
buf = rb_sprintf("%s=", name);
2265
failed = putenv(RSTRING_PTR(buf));
2266
/* even if putenv() failed, clean up and try to delete the
2267
* variable from the system area. */
2268
rb_str_resize(buf, 0);
2269
if (!value || !*value) {
2149
2270
/* putenv() doesn't handle empty value */
2151
failed = !SetEnvironmentVariable(name,value);
2154
len = strlen(name) + 1 + 1;
2155
buf = ALLOCA_N(char, len);
2156
snprintf(buf, len, "%s=", name);
2158
failed = !SetEnvironmentVariable(name, 0);
2161
rb_warn("failed to set environment variable. Ruby 1.9.3 will raise SystemCallError in this case.");
2271
if (!SetEnvironmentVariable(name, value) &&
2272
GetLastError() != ERROR_ENVVAR_NOT_FOUND) goto fail;
2274
if (failed) goto fail;
2163
2275
#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
2165
2277
#undef unsetenv
2246
2358
ruby_setenv(name, 0);
2364
* ENV.store(name, value) -> value
2366
* Sets the environment variable +name+ to +value+. If the value given is
2367
* +nil+ the environment variable is deleted.
2250
2371
env_aset(VALUE obj, VALUE nm, VALUE val)
2434
* ENV.each_key { |name| } -> Hash
2435
* ENV.each_key -> Enumerator
2437
* Yields each environment variable name.
2439
* An Enumerator is returned if no block is given.
2306
2442
env_each_key(VALUE ehash)
2483
* ENV.each_value { |value| } -> Hash
2484
* ENV.each_value -> Enumerator
2486
* Yields each environment variable +value+.
2488
* An Enumerator is returned if no block was given.
2340
2491
env_each_value(VALUE ehash)
2506
* ENV.each { |name, value| } -> Hash
2507
* ENV.each -> Enumerator
2508
* ENV.each_pair { |name, value| } -> Hash
2509
* ENV.each_pair -> Enumerator
2511
* Yields each environment variable +name+ and +value+.
2513
* If no block is given an Enumerator is returned.
2354
2516
env_each_pair(VALUE ehash)
2545
* ENV.reject! { |name, value| } -> Hash or nil
2546
* ENV.reject! -> Enumerator
2548
* Equivalent to ENV#delete_if but returns +nil+ if no changes were made.
2550
* Returns an Enumerator if no block was given.
2382
2553
env_reject_bang(VALUE ehash)
2577
* ENV.delete_if { |name, value| } -> Hash
2578
* ENV.delete_if -> Enumerator
2580
* Deletes every environment variable for which the block evaluates to +true+.
2582
* If no block is given an enumerator is returned instead.
2405
2585
env_delete_if(VALUE ehash)
2594
* ENV.values_at(name, ...) -> Array
2596
* Returns an array containing the environment variable values associated with
2597
* the given names. See also ENV.select.
2413
2600
env_values_at(int argc, VALUE *argv)
2615
* ENV.select { |name, value| } -> Hash
2616
* ENV.select -> Enumerator
2618
* Returns a copy of the environment for entries where the block returns true.
2620
* Returns an Enumerator if no block was given.
2427
2623
env_select(VALUE ehash)
2650
* ENV.select! { |name, value| } -> ENV or nil
2651
* ENV.select! -> Enumerator
2653
* Equivalent to ENV#keep_if but returns +nil+ if no changes were made.
2453
2656
env_select_bang(VALUE ehash)
2680
* ENV.keep_if { |name, value| } -> Hash
2681
* ENV.keep_if -> Enumerator
2683
* Deletes every environment variable where the block evaluates to +false+.
2685
* Returns an enumerator if no block was given.
2476
2688
env_keep_if(VALUE ehash)
2853
* ENV.key?(name) -> true or false
2854
* ENV.include?(name) -> true or false
2855
* ENV.has_key?(name) -> true or false
2856
* ENV.member?(name) -> true or false
2858
* Returns +true+ if there is an environment variable with the given +name+.
2593
2861
env_has_key(VALUE env, VALUE key)
2875
* ENV.assoc(name) -> Array or nil
2877
* Returns an Array of the name and value of the environment variable with
2878
* +name+ or +nil+ if the name cannot be found.
2606
2881
env_assoc(VALUE env, VALUE key)
2896
* ENV.value?(value) -> true or false
2897
* ENV.has_value?(value) -> true or false
2899
* Returns +true+ if there is an environment variable with the given +value+.
2620
2902
env_has_value(VALUE dmy, VALUE obj)
3032
* ENV.reject { |name, value| } -> Hash
3033
* ENV.reject -> Enumerator
3035
* Same as ENV#delete_if, but works on (and returns) a copy of the
2722
3039
env_reject(void)
2724
3041
return rb_hash_delete_if(env_to_hash());
3046
* ENV.shift -> Array or nil
3048
* Removes an environment variable name-value pair from ENV and returns it as
3049
* an Array. Returns +nil+ if when the environment is empty.
2728
3052
env_shift(void)
2791
3129
return ST_CONTINUE;
3134
* ENV.update(hash) -> Hash
3135
* ENV.update(hash) { |name, old_value, new_value| } -> Hash
3137
* Adds the contents of +hash+ to the environment variables. If no block is
3138
* specified entries with duplicate keys are overwritten, otherwise the value
3139
* of each duplicate name is determined by calling the block with the key, its
3140
* value from the environment and its value from the hash.
2795
3143
env_update(VALUE env, VALUE hash)
2893
3241
rb_define_method(rb_cHash,"compare_by_identity", rb_hash_compare_by_id, 0);
2894
3242
rb_define_method(rb_cHash,"compare_by_identity?", rb_hash_compare_by_id_p, 0);
3244
/* Document-class: ENV
3246
* ENV is a hash-like accessor for environment variables.
3250
* Hack to get RDoc to regard ENV as a class:
3251
* envtbl = rb_define_class("ENV", rb_cObject);
2896
3253
origenviron = environ;
2897
3254
envtbl = rb_obj_alloc(rb_cObject);
2898
3255
rb_extend_object(envtbl, rb_mEnumerable);
2939
3296
rb_define_singleton_method(envtbl,"assoc", env_assoc, 1);
2940
3297
rb_define_singleton_method(envtbl,"rassoc", env_rassoc, 1);
3300
* ENV is a Hash-like accessor for environment variables.
3302
* See ENV (the class) for more details.
2942
3304
rb_define_global_const("ENV", envtbl);