Ruby  2.0.0p353(2013-11-22revision43784)
hash.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  hash.c -
4 
5  $Author: nagachika $
6  created at: Mon Nov 22 18:51:18 JST 1993
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9  Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10  Copyright (C) 2000 Information-technology Promotion Agency, Japan
11 
12 **********************************************************************/
13 
14 #include "ruby/ruby.h"
15 #include "ruby/st.h"
16 #include "ruby/util.h"
17 #include "ruby/encoding.h"
18 #include "internal.h"
19 #include <errno.h>
20 #include "probes.h"
21 
22 #ifdef __APPLE__
23 # ifdef HAVE_CRT_EXTERNS_H
24 # include <crt_externs.h>
25 # else
26 # include "missing/crt_externs.h"
27 # endif
28 #endif
29 
31 
32 #define HASH_DELETED FL_USER1
33 #define HASH_PROC_DEFAULT FL_USER2
34 
35 VALUE
37 {
38  return rb_obj_freeze(hash);
39 }
40 
42 
43 static VALUE envtbl;
45 
46 static int
48 {
49  if (a == b) return 0;
50  if (FIXNUM_P(a) && FIXNUM_P(b)) {
51  return a != b;
52  }
53  if (RB_TYPE_P(a, T_STRING) && RBASIC(a)->klass == rb_cString &&
54  RB_TYPE_P(b, T_STRING) && RBASIC(b)->klass == rb_cString) {
55  return rb_str_hash_cmp(a, b);
56  }
57  if (a == Qundef || b == Qundef) return -1;
58  if (SYMBOL_P(a) && SYMBOL_P(b)) {
59  return a != b;
60  }
61 
62  return !rb_eql(a, b);
63 }
64 
65 VALUE
67 {
68  VALUE hval = rb_funcall(obj, id_hash, 0);
69  retry:
70  switch (TYPE(hval)) {
71  case T_FIXNUM:
72  return hval;
73 
74  case T_BIGNUM:
75  return LONG2FIX(((long*)(RBIGNUM_DIGITS(hval)))[0]);
76 
77  default:
78  hval = rb_to_int(hval);
79  goto retry;
80  }
81 }
82 
83 static st_index_t
85 {
86  VALUE hval;
87  st_index_t hnum;
88 
89  if (SPECIAL_CONST_P(a)) {
90  if (a == Qundef) return 0;
92  }
93  else if (BUILTIN_TYPE(a) == T_STRING) {
94  hnum = rb_str_hash(a);
95  }
96  else {
97  hval = rb_hash(a);
98  hnum = FIX2LONG(hval);
99  }
100  hnum <<= 1;
101  return (st_index_t)RSHIFT(hnum, 1);
102 }
103 
104 static const struct st_hash_type objhash = {
105  rb_any_cmp,
106  rb_any_hash,
107 };
108 
109 extern const struct st_hash_type st_hashtype_num;
110 #define identhash st_hashtype_num
111 
113 
118 };
119 
120 static int
122 {
123  int status;
124 
125  status = (*arg->func)(key, value, arg->arg);
126  if (status == ST_CONTINUE) {
127  return ST_CHECK;
128  }
129  return status;
130 }
131 
132 void
134 {
135  struct foreach_safe_arg arg;
136 
137  arg.tbl = table;
138  arg.func = (st_foreach_func *)func;
139  arg.arg = a;
140  if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
141  rb_raise(rb_eRuntimeError, "hash modified during iteration");
142  }
143 }
144 
146 
151 };
152 
153 static int
155 {
156  struct hash_foreach_arg *arg = (struct hash_foreach_arg *)argp;
157  int status;
158  st_table *tbl;
159 
160  tbl = RHASH(arg->hash)->ntbl;
161  status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg);
162  if (RHASH(arg->hash)->ntbl != tbl) {
163  rb_raise(rb_eRuntimeError, "rehash occurred during iteration");
164  }
165  switch (status) {
166  case ST_DELETE:
167  FL_SET(arg->hash, HASH_DELETED);
168  return ST_DELETE;
169  case ST_CONTINUE:
170  break;
171  case ST_STOP:
172  return ST_STOP;
173  }
174  return ST_CHECK;
175 }
176 
177 static VALUE
179 {
180  if (--RHASH_ITER_LEV(hash) == 0) {
181  if (FL_TEST(hash, HASH_DELETED)) {
182  st_cleanup_safe(RHASH(hash)->ntbl, (st_data_t)Qundef);
183  FL_UNSET(hash, HASH_DELETED);
184  }
185  }
186  return 0;
187 }
188 
189 static VALUE
191 {
192  VALUE hash = ((struct hash_foreach_arg *)arg)->hash;
194  rb_raise(rb_eRuntimeError, "hash modified during iteration");
195  }
196  return Qnil;
197 }
198 
199 void
201 {
202  struct hash_foreach_arg arg;
203 
204  if (!RHASH(hash)->ntbl)
205  return;
206  RHASH_ITER_LEV(hash)++;
207  arg.hash = hash;
208  arg.func = (rb_foreach_func *)func;
209  arg.arg = farg;
211 }
212 
213 static VALUE
215 {
216  NEWOBJ_OF(hash, struct RHash, klass, T_HASH);
217 
218  RHASH_IFNONE(hash) = Qnil;
219 
220  return (VALUE)hash;
221 }
222 
223 static VALUE
225 {
228  }
229 
230  return hash_alloc(klass);
231 }
232 
233 VALUE
235 {
236  return hash_alloc(rb_cHash);
237 }
238 
239 VALUE
241 {
242  NEWOBJ_OF(ret, struct RHash,
243  rb_obj_class(hash),
244  (RBASIC(hash)->flags)&(T_MASK|FL_EXIVAR|FL_TAINT|FL_UNTRUSTED));
245  if (FL_TEST((hash), FL_EXIVAR))
246  rb_copy_generic_ivar((VALUE)(ret),(VALUE)(hash));
247 
248  if (!RHASH_EMPTY_P(hash))
249  ret->ntbl = st_copy(RHASH(hash)->ntbl);
250  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
252  }
253  RHASH_IFNONE(ret) = RHASH_IFNONE(hash);
254  return (VALUE)ret;
255 }
256 
257 static void
259 {
260  rb_check_frozen(hash);
261  if (!OBJ_UNTRUSTED(hash) && rb_safe_level() >= 4)
262  rb_raise(rb_eSecurityError, "Insecure: can't modify hash");
263 }
264 
265 struct st_table *
267 {
268  if (!RHASH(hash)->ntbl) {
269  RHASH(hash)->ntbl = st_init_table(&objhash);
270  }
271  return RHASH(hash)->ntbl;
272 }
273 
274 static void
276 {
277  rb_hash_modify_check(hash);
278  rb_hash_tbl(hash);
279 }
280 
281 NORETURN(static void no_new_key(void));
282 static void
284 {
285  rb_raise(rb_eRuntimeError, "can't add a new key into hash during iteration");
286 }
287 
288 #define NOINSERT_UPDATE_CALLBACK(func) \
289 int \
290 func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
291 { \
292  if (!existing) no_new_key(); \
293  return func(key, val, arg, existing); \
294 }
295 
296 #define UPDATE_CALLBACK(iter_lev, func) ((iter_lev) > 0 ? func##_noinsert : func)
297 
298 #define RHASH_UPDATE_ITER(hash, iter_lev, key, func, arg) \
299  st_update(RHASH(hash)->ntbl, (st_data_t)(key), \
300  UPDATE_CALLBACK((iter_lev), func), \
301  (st_data_t)(arg))
302 #define RHASH_UPDATE(hash, key, func, arg) \
303  RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
304 
305 static void
307 {
308  int n = rb_proc_arity(proc);
309 
310  if (rb_proc_lambda_p(proc) && n != 2 && (n >= 0 || n < -3)) {
311  if (n < 0) n = -n-1;
312  rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n);
313  }
314 }
315 
316 /*
317  * call-seq:
318  * Hash.new -> new_hash
319  * Hash.new(obj) -> new_hash
320  * Hash.new {|hash, key| block } -> new_hash
321  *
322  * Returns a new, empty hash. If this hash is subsequently accessed by
323  * a key that doesn't correspond to a hash entry, the value returned
324  * depends on the style of <code>new</code> used to create the hash. In
325  * the first form, the access returns <code>nil</code>. If
326  * <i>obj</i> is specified, this single object will be used for
327  * all <em>default values</em>. If a block is specified, it will be
328  * called with the hash object and the key, and should return the
329  * default value. It is the block's responsibility to store the value
330  * in the hash if required.
331  *
332  * h = Hash.new("Go Fish")
333  * h["a"] = 100
334  * h["b"] = 200
335  * h["a"] #=> 100
336  * h["c"] #=> "Go Fish"
337  * # The following alters the single default object
338  * h["c"].upcase! #=> "GO FISH"
339  * h["d"] #=> "GO FISH"
340  * h.keys #=> ["a", "b"]
341  *
342  * # While this creates a new default object each time
343  * h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
344  * h["c"] #=> "Go Fish: c"
345  * h["c"].upcase! #=> "GO FISH: C"
346  * h["d"] #=> "Go Fish: d"
347  * h.keys #=> ["c", "d"]
348  *
349  */
350 
351 static VALUE
353 {
354  VALUE ifnone;
355 
356  rb_hash_modify(hash);
357  if (rb_block_given_p()) {
358  rb_check_arity(argc, 0, 0);
359  ifnone = rb_block_proc();
360  default_proc_arity_check(ifnone);
361  RHASH_IFNONE(hash) = ifnone;
362  FL_SET(hash, HASH_PROC_DEFAULT);
363  }
364  else {
365  rb_scan_args(argc, argv, "01", &ifnone);
366  RHASH_IFNONE(hash) = ifnone;
367  }
368 
369  return hash;
370 }
371 
372 /*
373  * call-seq:
374  * Hash[ key, value, ... ] -> new_hash
375  * Hash[ [ [key, value], ... ] ] -> new_hash
376  * Hash[ object ] -> new_hash
377  *
378  * Creates a new hash populated with the given objects. Equivalent to
379  * the literal <code>{ <i>key</i> => <i>value</i>, ... }</code>. In the first
380  * form, keys and values occur in pairs, so there must be an even number of arguments.
381  * The second and third form take a single argument which is either
382  * an array of key-value pairs or an object convertible to a hash.
383  *
384  * Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}
385  * Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200}
386  * Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
387  */
388 
389 static VALUE
391 {
392  VALUE hash, tmp;
393  int i;
394 
395  if (argc == 1) {
396  tmp = rb_hash_s_try_convert(Qnil, argv[0]);
397  if (!NIL_P(tmp)) {
398  hash = hash_alloc(klass);
399  if (RHASH(tmp)->ntbl) {
400  RHASH(hash)->ntbl = st_copy(RHASH(tmp)->ntbl);
401  }
402  return hash;
403  }
404 
405  tmp = rb_check_array_type(argv[0]);
406  if (!NIL_P(tmp)) {
407  long i;
408 
409  hash = hash_alloc(klass);
410  for (i = 0; i < RARRAY_LEN(tmp); ++i) {
411  VALUE e = RARRAY_PTR(tmp)[i];
413  VALUE key, val = Qnil;
414 
415  if (NIL_P(v)) {
416 #if 0 /* refix in the next release */
417  rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)",
418  rb_builtin_class_name(e), i);
419 
420 #else
421  rb_warn("wrong element type %s at %ld (expected array)",
422  rb_builtin_class_name(e), i);
423  rb_warn("ignoring wrong elements is deprecated, remove them explicitly");
424  rb_warn("this causes ArgumentError in the next release");
425  continue;
426 #endif
427  }
428  switch (RARRAY_LEN(v)) {
429  default:
430  rb_raise(rb_eArgError, "invalid number of elements (%ld for 1..2)",
431  RARRAY_LEN(v));
432  case 2:
433  val = RARRAY_PTR(v)[1];
434  case 1:
435  key = RARRAY_PTR(v)[0];
436  rb_hash_aset(hash, key, val);
437  }
438  }
439  return hash;
440  }
441  }
442  if (argc % 2 != 0) {
443  rb_raise(rb_eArgError, "odd number of arguments for Hash");
444  }
445 
446  hash = hash_alloc(klass);
447  for (i=0; i<argc; i+=2) {
448  rb_hash_aset(hash, argv[i], argv[i + 1]);
449  }
450 
451  return hash;
452 }
453 
454 static VALUE
456 {
457  return rb_convert_type(hash, T_HASH, "Hash", "to_hash");
458 }
459 
460 VALUE
462 {
463  return rb_check_convert_type(hash, T_HASH, "Hash", "to_hash");
464 }
465 
466 /*
467  * call-seq:
468  * Hash.try_convert(obj) -> hash or nil
469  *
470  * Try to convert <i>obj</i> into a hash, using to_hash method.
471  * Returns converted hash or nil if <i>obj</i> cannot be converted
472  * for any reason.
473  *
474  * Hash.try_convert({1=>2}) # => {1=>2}
475  * Hash.try_convert("1=>2") # => nil
476  */
477 static VALUE
479 {
480  return rb_check_hash_type(hash);
481 }
482 
483 static int
485 {
486  st_table *tbl = (st_table *)arg;
487 
488  st_insert(tbl, (st_data_t)key, (st_data_t)value);
489  return ST_CONTINUE;
490 }
491 
492 /*
493  * call-seq:
494  * hsh.rehash -> hsh
495  *
496  * Rebuilds the hash based on the current hash values for each key. If
497  * values of key objects have changed since they were inserted, this
498  * method will reindex <i>hsh</i>. If <code>Hash#rehash</code> is
499  * called while an iterator is traversing the hash, an
500  * <code>RuntimeError</code> will be raised in the iterator.
501  *
502  * a = [ "a", "b" ]
503  * c = [ "c", "d" ]
504  * h = { a => 100, c => 300 }
505  * h[a] #=> 100
506  * a[0] = "z"
507  * h[a] #=> nil
508  * h.rehash #=> {["z", "b"]=>100, ["c", "d"]=>300}
509  * h[a] #=> 100
510  */
511 
512 static VALUE
514 {
515  st_table *tbl;
516 
517  if (RHASH_ITER_LEV(hash) > 0) {
518  rb_raise(rb_eRuntimeError, "rehash during iteration");
519  }
520  rb_hash_modify_check(hash);
521  if (!RHASH(hash)->ntbl)
522  return hash;
523  tbl = st_init_table_with_size(RHASH(hash)->ntbl->type, RHASH(hash)->ntbl->num_entries);
525  st_free_table(RHASH(hash)->ntbl);
526  RHASH(hash)->ntbl = tbl;
527 
528  return hash;
529 }
530 
531 static VALUE
533 {
535  VALUE ifnone = RHASH_IFNONE(hash);
536  if (!FL_TEST(hash, HASH_PROC_DEFAULT)) return ifnone;
537  if (key == Qundef) return Qnil;
538  return rb_funcall(ifnone, id_yield, 2, hash, key);
539  }
540  else {
541  return rb_funcall(hash, id_default, 1, key);
542  }
543 }
544 
545 /*
546  * call-seq:
547  * hsh[key] -> value
548  *
549  * Element Reference---Retrieves the <i>value</i> object corresponding
550  * to the <i>key</i> object. If not found, returns the default value (see
551  * <code>Hash::new</code> for details).
552  *
553  * h = { "a" => 100, "b" => 200 }
554  * h["a"] #=> 100
555  * h["c"] #=> nil
556  *
557  */
558 
559 VALUE
561 {
562  st_data_t val;
563 
564  if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
565  return hash_default_value(hash, key);
566  }
567  return (VALUE)val;
568 }
569 
570 VALUE
572 {
573  st_data_t val;
574 
575  if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
576  return def; /* without Hash#default */
577  }
578  return (VALUE)val;
579 }
580 
581 VALUE
583 {
584  return rb_hash_lookup2(hash, key, Qnil);
585 }
586 
587 /*
588  * call-seq:
589  * hsh.fetch(key [, default] ) -> obj
590  * hsh.fetch(key) {| key | block } -> obj
591  *
592  * Returns a value from the hash for the given key. If the key can't be
593  * found, there are several options: With no other arguments, it will
594  * raise an <code>KeyError</code> exception; if <i>default</i> is
595  * given, then that will be returned; if the optional code block is
596  * specified, then that will be run and its result returned.
597  *
598  * h = { "a" => 100, "b" => 200 }
599  * h.fetch("a") #=> 100
600  * h.fetch("z", "go fish") #=> "go fish"
601  * h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
602  *
603  * The following example shows that an exception is raised if the key
604  * is not found and a default value is not supplied.
605  *
606  * h = { "a" => 100, "b" => 200 }
607  * h.fetch("z")
608  *
609  * <em>produces:</em>
610  *
611  * prog.rb:2:in `fetch': key not found (KeyError)
612  * from prog.rb:2
613  *
614  */
615 
616 static VALUE
618 {
619  VALUE key, if_none;
620  st_data_t val;
621  long block_given;
622 
623  rb_scan_args(argc, argv, "11", &key, &if_none);
624 
625  block_given = rb_block_given_p();
626  if (block_given && argc == 2) {
627  rb_warn("block supersedes default value argument");
628  }
629  if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
630  if (block_given) return rb_yield(key);
631  if (argc == 1) {
632  volatile VALUE desc = rb_protect(rb_inspect, key, 0);
633  if (NIL_P(desc)) {
634  desc = rb_any_to_s(key);
635  }
636  desc = rb_str_ellipsize(desc, 65);
637  rb_raise(rb_eKeyError, "key not found: %s", RSTRING_PTR(desc));
638  }
639  return if_none;
640  }
641  return (VALUE)val;
642 }
643 
644 VALUE
646 {
647  return rb_hash_fetch_m(1, &key, hash);
648 }
649 
650 /*
651  * call-seq:
652  * hsh.default(key=nil) -> obj
653  *
654  * Returns the default value, the value that would be returned by
655  * <i>hsh</i>[<i>key</i>] if <i>key</i> did not exist in <i>hsh</i>.
656  * See also <code>Hash::new</code> and <code>Hash#default=</code>.
657  *
658  * h = Hash.new #=> {}
659  * h.default #=> nil
660  * h.default(2) #=> nil
661  *
662  * h = Hash.new("cat") #=> {}
663  * h.default #=> "cat"
664  * h.default(2) #=> "cat"
665  *
666  * h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}
667  * h.default #=> nil
668  * h.default(2) #=> 20
669  */
670 
671 static VALUE
673 {
674  VALUE key, ifnone;
675 
676  rb_scan_args(argc, argv, "01", &key);
677  ifnone = RHASH_IFNONE(hash);
678  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
679  if (argc == 0) return Qnil;
680  return rb_funcall(ifnone, id_yield, 2, hash, key);
681  }
682  return ifnone;
683 }
684 
685 /*
686  * call-seq:
687  * hsh.default = obj -> obj
688  *
689  * Sets the default value, the value returned for a key that does not
690  * exist in the hash. It is not possible to set the default to a
691  * <code>Proc</code> that will be executed on each key lookup.
692  *
693  * h = { "a" => 100, "b" => 200 }
694  * h.default = "Go fish"
695  * h["a"] #=> 100
696  * h["z"] #=> "Go fish"
697  * # This doesn't do what you might hope...
698  * h.default = proc do |hash, key|
699  * hash[key] = key + key
700  * end
701  * h[2] #=> #<Proc:0x401b3948@-:6>
702  * h["cat"] #=> #<Proc:0x401b3948@-:6>
703  */
704 
705 static VALUE
707 {
708  rb_hash_modify_check(hash);
709  RHASH_IFNONE(hash) = ifnone;
711  return ifnone;
712 }
713 
714 /*
715  * call-seq:
716  * hsh.default_proc -> anObject
717  *
718  * If <code>Hash::new</code> was invoked with a block, return that
719  * block, otherwise return <code>nil</code>.
720  *
721  * h = Hash.new {|h,k| h[k] = k*k } #=> {}
722  * p = h.default_proc #=> #<Proc:0x401b3d08@-:1>
723  * a = [] #=> []
724  * p.call(a, 2)
725  * a #=> [nil, nil, 4]
726  */
727 
728 
729 static VALUE
731 {
732  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
733  return RHASH_IFNONE(hash);
734  }
735  return Qnil;
736 }
737 
738 /*
739  * call-seq:
740  * hsh.default_proc = proc_obj or nil
741  *
742  * Sets the default proc to be executed on each failed key lookup.
743  *
744  * h.default_proc = proc do |hash, key|
745  * hash[key] = key + key
746  * end
747  * h[2] #=> 4
748  * h["cat"] #=> "catcat"
749  */
750 
751 static VALUE
753 {
754  VALUE b;
755 
756  rb_hash_modify_check(hash);
757  if (NIL_P(proc)) {
759  RHASH_IFNONE(hash) = proc;
760  return proc;
761  }
762  b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
763  if (NIL_P(b) || !rb_obj_is_proc(b)) {
765  "wrong default_proc type %s (expected Proc)",
766  rb_obj_classname(proc));
767  }
768  proc = b;
770  RHASH_IFNONE(hash) = proc;
771  FL_SET(hash, HASH_PROC_DEFAULT);
772  return proc;
773 }
774 
775 static int
776 key_i(VALUE key, VALUE value, VALUE arg)
777 {
778  VALUE *args = (VALUE *)arg;
779 
780  if (rb_equal(value, args[0])) {
781  args[1] = key;
782  return ST_STOP;
783  }
784  return ST_CONTINUE;
785 }
786 
787 /*
788  * call-seq:
789  * hsh.key(value) -> key
790  *
791  * Returns the key of an occurrence of a given value. If the value is
792  * not found, returns <code>nil</code>.
793  *
794  * h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
795  * h.key(200) #=> "b"
796  * h.key(300) #=> "c"
797  * h.key(999) #=> nil
798  *
799  */
800 
801 static VALUE
802 rb_hash_key(VALUE hash, VALUE value)
803 {
804  VALUE args[2];
805 
806  args[0] = value;
807  args[1] = Qnil;
808 
809  rb_hash_foreach(hash, key_i, (VALUE)args);
810 
811  return args[1];
812 }
813 
814 /* :nodoc: */
815 static VALUE
817 {
818  rb_warn("Hash#index is deprecated; use Hash#key");
819  return rb_hash_key(hash, value);
820 }
821 
822 static VALUE
824 {
825  st_data_t ktmp = (st_data_t)key, val;
826 
827  if (!RHASH(hash)->ntbl)
828  return Qundef;
829  if (RHASH_ITER_LEV(hash) > 0) {
830  if (st_delete_safe(RHASH(hash)->ntbl, &ktmp, &val, (st_data_t)Qundef)) {
831  FL_SET(hash, HASH_DELETED);
832  return (VALUE)val;
833  }
834  }
835  else if (st_delete(RHASH(hash)->ntbl, &ktmp, &val))
836  return (VALUE)val;
837  return Qundef;
838 }
839 
840 /*
841  * call-seq:
842  * hsh.delete(key) -> value
843  * hsh.delete(key) {| key | block } -> value
844  *
845  * Deletes the key-value pair and returns the value from <i>hsh</i> whose
846  * key is equal to <i>key</i>. If the key is not found, returns the
847  * <em>default value</em>. If the optional code block is given and the
848  * key is not found, pass in the key and return the result of
849  * <i>block</i>.
850  *
851  * h = { "a" => 100, "b" => 200 }
852  * h.delete("a") #=> 100
853  * h.delete("z") #=> nil
854  * h.delete("z") { |el| "#{el} not found" } #=> "z not found"
855  *
856  */
857 
858 VALUE
860 {
861  VALUE val;
862 
863  rb_hash_modify_check(hash);
864  val = rb_hash_delete_key(hash, key);
865  if (val != Qundef) return val;
866  if (rb_block_given_p()) {
867  return rb_yield(key);
868  }
869  return Qnil;
870 }
871 
872 struct shift_var {
875 };
876 
877 static int
879 {
880  struct shift_var *var = (struct shift_var *)arg;
881 
882  if (var->key != Qundef) return ST_STOP;
883  var->key = key;
884  var->val = value;
885  return ST_DELETE;
886 }
887 
888 static int
890 {
891  struct shift_var *var = (struct shift_var *)arg;
892 
893  var->key = key;
894  var->val = value;
895  return ST_STOP;
896 }
897 
898 /*
899  * call-seq:
900  * hsh.shift -> anArray or obj
901  *
902  * Removes a key-value pair from <i>hsh</i> and returns it as the
903  * two-item array <code>[</code> <i>key, value</i> <code>]</code>, or
904  * the hash's default value if the hash is empty.
905  *
906  * h = { 1 => "a", 2 => "b", 3 => "c" }
907  * h.shift #=> [1, "a"]
908  * h #=> {2=>"b", 3=>"c"}
909  */
910 
911 static VALUE
913 {
914  struct shift_var var;
915 
916  rb_hash_modify_check(hash);
917  if (RHASH(hash)->ntbl) {
918  var.key = Qundef;
920  (VALUE)&var);
921 
922  if (var.key != Qundef) {
923  if (RHASH_ITER_LEV(hash) > 0) {
924  rb_hash_delete_key(hash, var.key);
925  }
926  return rb_assoc_new(var.key, var.val);
927  }
928  }
929  return hash_default_value(hash, Qnil);
930 }
931 
932 static int
934 {
935  if (RTEST(rb_yield_values(2, key, value))) {
936  rb_hash_delete_key(hash, key);
937  }
938  return ST_CONTINUE;
939 }
940 
941 static VALUE rb_hash_size(VALUE hash);
942 
943 /*
944  * call-seq:
945  * hsh.delete_if {| key, value | block } -> hsh
946  * hsh.delete_if -> an_enumerator
947  *
948  * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
949  * evaluates to <code>true</code>.
950  *
951  * If no block is given, an enumerator is returned instead.
952  *
953  * h = { "a" => 100, "b" => 200, "c" => 300 }
954  * h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}
955  *
956  */
957 
958 VALUE
960 {
962  rb_hash_modify_check(hash);
963  if (RHASH(hash)->ntbl)
964  rb_hash_foreach(hash, delete_if_i, hash);
965  return hash;
966 }
967 
968 /*
969  * call-seq:
970  * hsh.reject! {| key, value | block } -> hsh or nil
971  * hsh.reject! -> an_enumerator
972  *
973  * Equivalent to <code>Hash#delete_if</code>, but returns
974  * <code>nil</code> if no changes were made.
975  */
976 
977 VALUE
979 {
980  st_index_t n;
981 
983  rb_hash_modify(hash);
984  if (!RHASH(hash)->ntbl)
985  return Qnil;
986  n = RHASH(hash)->ntbl->num_entries;
987  rb_hash_foreach(hash, delete_if_i, hash);
988  if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
989  return hash;
990 }
991 
992 /*
993  * call-seq:
994  * hsh.reject {| key, value | block } -> a_hash
995  * hsh.reject -> an_enumerator
996  *
997  * Same as <code>Hash#delete_if</code>, but works on (and returns) a
998  * copy of the <i>hsh</i>. Equivalent to
999  * <code><i>hsh</i>.dup.delete_if</code>.
1000  *
1001  */
1002 
1003 static VALUE
1005 {
1006  return rb_hash_delete_if(rb_obj_dup(hash));
1007 }
1008 
1009 /*
1010  * call-seq:
1011  * hsh.values_at(key, ...) -> array
1012  *
1013  * Return an array containing the values associated with the given keys.
1014  * Also see <code>Hash.select</code>.
1015  *
1016  * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
1017  * h.values_at("cow", "cat") #=> ["bovine", "feline"]
1018  */
1019 
1020 VALUE
1022 {
1023  VALUE result = rb_ary_new2(argc);
1024  long i;
1025 
1026  for (i=0; i<argc; i++) {
1027  rb_ary_push(result, rb_hash_aref(hash, argv[i]));
1028  }
1029  return result;
1030 }
1031 
1032 static int
1034 {
1035  if (RTEST(rb_yield_values(2, key, value)))
1036  rb_hash_aset(result, key, value);
1037  return ST_CONTINUE;
1038 }
1039 
1040 /*
1041  * call-seq:
1042  * hsh.select {|key, value| block} -> a_hash
1043  * hsh.select -> an_enumerator
1044  *
1045  * Returns a new hash consisting of entries for which the block returns true.
1046  *
1047  * If no block is given, an enumerator is returned instead.
1048  *
1049  * h = { "a" => 100, "b" => 200, "c" => 300 }
1050  * h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
1051  * h.select {|k,v| v < 200} #=> {"a" => 100}
1052  */
1053 
1054 VALUE
1056 {
1057  VALUE result;
1058 
1060  result = rb_hash_new();
1061  rb_hash_foreach(hash, select_i, result);
1062  return result;
1063 }
1064 
1065 static int
1067 {
1068  if (!RTEST(rb_yield_values(2, key, value))) {
1069  return ST_DELETE;
1070  }
1071  return ST_CONTINUE;
1072 }
1073 
1074 /*
1075  * call-seq:
1076  * hsh.select! {| key, value | block } -> hsh or nil
1077  * hsh.select! -> an_enumerator
1078  *
1079  * Equivalent to <code>Hash#keep_if</code>, but returns
1080  * <code>nil</code> if no changes were made.
1081  */
1082 
1083 VALUE
1085 {
1086  st_index_t n;
1087 
1089  rb_hash_modify_check(hash);
1090  if (!RHASH(hash)->ntbl)
1091  return Qnil;
1092  n = RHASH(hash)->ntbl->num_entries;
1093  rb_hash_foreach(hash, keep_if_i, hash);
1094  if (n == RHASH(hash)->ntbl->num_entries) return Qnil;
1095  return hash;
1096 }
1097 
1098 /*
1099  * call-seq:
1100  * hsh.keep_if {| key, value | block } -> hsh
1101  * hsh.keep_if -> an_enumerator
1102  *
1103  * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
1104  * evaluates to false.
1105  *
1106  * If no block is given, an enumerator is returned instead.
1107  *
1108  */
1109 
1110 VALUE
1112 {
1114  rb_hash_modify_check(hash);
1115  if (RHASH(hash)->ntbl)
1116  rb_hash_foreach(hash, keep_if_i, hash);
1117  return hash;
1118 }
1119 
1120 static int
1121 clear_i(VALUE key, VALUE value, VALUE dummy)
1122 {
1123  return ST_DELETE;
1124 }
1125 
1126 /*
1127  * call-seq:
1128  * hsh.clear -> hsh
1129  *
1130  * Removes all key-value pairs from <i>hsh</i>.
1131  *
1132  * h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200}
1133  * h.clear #=> {}
1134  *
1135  */
1136 
1137 VALUE
1139 {
1140  rb_hash_modify_check(hash);
1141  if (!RHASH(hash)->ntbl)
1142  return hash;
1143  if (RHASH(hash)->ntbl->num_entries > 0) {
1144  if (RHASH_ITER_LEV(hash) > 0)
1145  rb_hash_foreach(hash, clear_i, 0);
1146  else
1147  st_clear(RHASH(hash)->ntbl);
1148  }
1149 
1150  return hash;
1151 }
1152 
1153 static int
1155 {
1156  *val = arg;
1157  return ST_CONTINUE;
1158 }
1159 
1160 static int
1162 {
1163  *key = (st_data_t)rb_str_new_frozen((VALUE)*key);
1164  return hash_aset(key, val, arg, existing);
1165 }
1166 
1169 
1170 /*
1171  * call-seq:
1172  * hsh[key] = value -> value
1173  * hsh.store(key, value) -> value
1174  *
1175  * == Element Assignment
1176  *
1177  * Associates the value given by +value+ with the key given by +key+.
1178  *
1179  * h = { "a" => 100, "b" => 200 }
1180  * h["a"] = 9
1181  * h["c"] = 4
1182  * h #=> {"a"=>9, "b"=>200, "c"=>4}
1183  *
1184  * +key+ should not have its value changed while it is in use as a key (an
1185  * <tt>unfrozen String</tt> passed as a key will be duplicated and frozen).
1186  *
1187  * a = "a"
1188  * b = "b".freeze
1189  * h = { a => 100, b => 200 }
1190  * h.key(100).equal? a #=> false
1191  * h.key(200).equal? b #=> true
1192  *
1193  */
1194 
1195 VALUE
1197 {
1198  int iter_lev = RHASH_ITER_LEV(hash);
1199  st_table *tbl = RHASH(hash)->ntbl;
1200 
1201  rb_hash_modify(hash);
1202  if (!tbl) {
1203  if (iter_lev > 0) no_new_key();
1204  tbl = RHASH_TBL(hash);
1205  }
1206  if (tbl->type == &identhash || rb_obj_class(key) != rb_cString) {
1207  RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
1208  }
1209  else {
1210  RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
1211  }
1212  return val;
1213 }
1214 
1215 static int
1217 {
1218  rb_hash_aset(hash, key, val);
1219 
1220  return ST_CONTINUE;
1221 }
1222 
1223 static VALUE
1225 {
1226  rb_hash_modify_check(hash);
1227  hash2 = to_hash(hash2);
1228 
1229  Check_Type(hash2, T_HASH);
1230 
1231  if (!RHASH_EMPTY_P(hash2)) {
1232  RHASH(hash)->ntbl = st_copy(RHASH(hash2)->ntbl);
1233  rb_hash_rehash(hash);
1234  }
1235 
1236  if (FL_TEST(hash2, HASH_PROC_DEFAULT)) {
1237  FL_SET(hash, HASH_PROC_DEFAULT);
1238  }
1239  else {
1240  FL_UNSET(hash, HASH_PROC_DEFAULT);
1241  }
1242  RHASH_IFNONE(hash) = RHASH_IFNONE(hash2);
1243 
1244  return hash;
1245 }
1246 
1247 /*
1248  * call-seq:
1249  * hsh.replace(other_hash) -> hsh
1250  *
1251  * Replaces the contents of <i>hsh</i> with the contents of
1252  * <i>other_hash</i>.
1253  *
1254  * h = { "a" => 100, "b" => 200 }
1255  * h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
1256  *
1257  */
1258 
1259 static VALUE
1261 {
1262  rb_hash_modify_check(hash);
1263  hash2 = to_hash(hash2);
1264  if (hash == hash2) return hash;
1265  rb_hash_clear(hash);
1266  if (RHASH(hash2)->ntbl) {
1267  rb_hash_tbl(hash);
1268  RHASH(hash)->ntbl->type = RHASH(hash2)->ntbl->type;
1269  }
1270  rb_hash_foreach(hash2, replace_i, hash);
1271  RHASH_IFNONE(hash) = RHASH_IFNONE(hash2);
1272  if (FL_TEST(hash2, HASH_PROC_DEFAULT)) {
1273  FL_SET(hash, HASH_PROC_DEFAULT);
1274  }
1275  else {
1276  FL_UNSET(hash, HASH_PROC_DEFAULT);
1277  }
1278 
1279  return hash;
1280 }
1281 
1282 /*
1283  * call-seq:
1284  * hsh.length -> fixnum
1285  * hsh.size -> fixnum
1286  *
1287  * Returns the number of key-value pairs in the hash.
1288  *
1289  * h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
1290  * h.length #=> 4
1291  * h.delete("a") #=> 200
1292  * h.length #=> 3
1293  */
1294 
1295 static VALUE
1297 {
1298  if (!RHASH(hash)->ntbl)
1299  return INT2FIX(0);
1300  return INT2FIX(RHASH(hash)->ntbl->num_entries);
1301 }
1302 
1303 
1304 /*
1305  * call-seq:
1306  * hsh.empty? -> true or false
1307  *
1308  * Returns <code>true</code> if <i>hsh</i> contains no key-value pairs.
1309  *
1310  * {}.empty? #=> true
1311  *
1312  */
1313 
1314 static VALUE
1316 {
1317  return RHASH_EMPTY_P(hash) ? Qtrue : Qfalse;
1318 }
1319 
1320 static int
1322 {
1323  rb_yield(value);
1324  return ST_CONTINUE;
1325 }
1326 
1327 /*
1328  * call-seq:
1329  * hsh.each_value {| value | block } -> hsh
1330  * hsh.each_value -> an_enumerator
1331  *
1332  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the
1333  * value as a parameter.
1334  *
1335  * If no block is given, an enumerator is returned instead.
1336  *
1337  * h = { "a" => 100, "b" => 200 }
1338  * h.each_value {|value| puts value }
1339  *
1340  * <em>produces:</em>
1341  *
1342  * 100
1343  * 200
1344  */
1345 
1346 static VALUE
1348 {
1350  rb_hash_foreach(hash, each_value_i, 0);
1351  return hash;
1352 }
1353 
1354 static int
1356 {
1357  rb_yield(key);
1358  return ST_CONTINUE;
1359 }
1360 
1361 /*
1362  * call-seq:
1363  * hsh.each_key {| key | block } -> hsh
1364  * hsh.each_key -> an_enumerator
1365  *
1366  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key
1367  * as a parameter.
1368  *
1369  * If no block is given, an enumerator is returned instead.
1370  *
1371  * h = { "a" => 100, "b" => 200 }
1372  * h.each_key {|key| puts key }
1373  *
1374  * <em>produces:</em>
1375  *
1376  * a
1377  * b
1378  */
1379 static VALUE
1381 {
1383  rb_hash_foreach(hash, each_key_i, 0);
1384  return hash;
1385 }
1386 
1387 static int
1389 {
1390  rb_yield(rb_assoc_new(key, value));
1391  return ST_CONTINUE;
1392 }
1393 
1394 /*
1395  * call-seq:
1396  * hsh.each {| key, value | block } -> hsh
1397  * hsh.each_pair {| key, value | block } -> hsh
1398  * hsh.each -> an_enumerator
1399  * hsh.each_pair -> an_enumerator
1400  *
1401  * Calls <i>block</i> once for each key in <i>hsh</i>, passing the key-value
1402  * pair as parameters.
1403  *
1404  * If no block is given, an enumerator is returned instead.
1405  *
1406  * h = { "a" => 100, "b" => 200 }
1407  * h.each {|key, value| puts "#{key} is #{value}" }
1408  *
1409  * <em>produces:</em>
1410  *
1411  * a is 100
1412  * b is 200
1413  *
1414  */
1415 
1416 static VALUE
1418 {
1420  rb_hash_foreach(hash, each_pair_i, 0);
1421  return hash;
1422 }
1423 
1424 static int
1426 {
1427  rb_ary_push(ary, rb_assoc_new(key, value));
1428  return ST_CONTINUE;
1429 }
1430 
1431 /*
1432  * call-seq:
1433  * hsh.to_a -> array
1434  *
1435  * Converts <i>hsh</i> to a nested array of <code>[</code> <i>key,
1436  * value</i> <code>]</code> arrays.
1437  *
1438  * h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
1439  * h.to_a #=> [["c", 300], ["a", 100], ["d", 400]]
1440  */
1441 
1442 static VALUE
1444 {
1445  VALUE ary;
1446 
1447  ary = rb_ary_new();
1448  rb_hash_foreach(hash, to_a_i, ary);
1449  OBJ_INFECT(ary, hash);
1450 
1451  return ary;
1452 }
1453 
1454 static int
1456 {
1457  VALUE str2;
1458 
1459  str2 = rb_inspect(key);
1460  if (RSTRING_LEN(str) > 1) {
1461  rb_str_buf_cat_ascii(str, ", ");
1462  }
1463  else {
1464  rb_enc_copy(str, str2);
1465  }
1466  rb_str_buf_append(str, str2);
1467  OBJ_INFECT(str, str2);
1468  rb_str_buf_cat_ascii(str, "=>");
1469  str2 = rb_inspect(value);
1470  rb_str_buf_append(str, str2);
1471  OBJ_INFECT(str, str2);
1472 
1473  return ST_CONTINUE;
1474 }
1475 
1476 static VALUE
1477 inspect_hash(VALUE hash, VALUE dummy, int recur)
1478 {
1479  VALUE str;
1480 
1481  if (recur) return rb_usascii_str_new2("{...}");
1482  str = rb_str_buf_new2("{");
1483  rb_hash_foreach(hash, inspect_i, str);
1484  rb_str_buf_cat2(str, "}");
1485  OBJ_INFECT(str, hash);
1486 
1487  return str;
1488 }
1489 
1490 /*
1491  * call-seq:
1492  * hsh.to_s -> string
1493  * hsh.inspect -> string
1494  *
1495  * Return the contents of this hash as a string.
1496  *
1497  * h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
1498  * h.to_s #=> "{\"c\"=>300, \"a\"=>100, \"d\"=>400}"
1499  */
1500 
1501 static VALUE
1503 {
1504  if (RHASH_EMPTY_P(hash))
1505  return rb_usascii_str_new2("{}");
1506  return rb_exec_recursive(inspect_hash, hash, 0);
1507 }
1508 
1509 /*
1510  * call-seq:
1511  * hsh.to_hash => hsh
1512  *
1513  * Returns +self+.
1514  */
1515 
1516 static VALUE
1518 {
1519  return hash;
1520 }
1521 
1522 /*
1523  * call-seq:
1524  * hsh.to_h -> hsh or new_hash
1525  *
1526  * Returns +self+. If called on a subclass of Hash, converts
1527  * the receiver to a Hash object.
1528  */
1529 
1530 static VALUE
1532 {
1533  if (rb_obj_class(hash) != rb_cHash) {
1534  VALUE ret = rb_hash_new();
1535  if (!RHASH_EMPTY_P(hash))
1536  RHASH(ret)->ntbl = st_copy(RHASH(hash)->ntbl);
1537  if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
1538  FL_SET(ret, HASH_PROC_DEFAULT);
1539  }
1540  RHASH_IFNONE(ret) = RHASH_IFNONE(hash);
1541  return ret;
1542  }
1543  return hash;
1544 }
1545 
1546 static int
1548 {
1549  rb_ary_push(ary, key);
1550  return ST_CONTINUE;
1551 }
1552 
1553 /*
1554  * call-seq:
1555  * hsh.keys -> array
1556  *
1557  * Returns a new array populated with the keys from this hash. See also
1558  * <code>Hash#values</code>.
1559  *
1560  * h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }
1561  * h.keys #=> ["a", "b", "c", "d"]
1562  *
1563  */
1564 
1565 static VALUE
1567 {
1568  VALUE ary;
1569 
1570  ary = rb_ary_new();
1571  rb_hash_foreach(hash, keys_i, ary);
1572 
1573  return ary;
1574 }
1575 
1576 static int
1578 {
1579  rb_ary_push(ary, value);
1580  return ST_CONTINUE;
1581 }
1582 
1583 /*
1584  * call-seq:
1585  * hsh.values -> array
1586  *
1587  * Returns a new array populated with the values from <i>hsh</i>. See
1588  * also <code>Hash#keys</code>.
1589  *
1590  * h = { "a" => 100, "b" => 200, "c" => 300 }
1591  * h.values #=> [100, 200, 300]
1592  *
1593  */
1594 
1595 static VALUE
1597 {
1598  VALUE ary;
1599 
1600  ary = rb_ary_new();
1601  rb_hash_foreach(hash, values_i, ary);
1602 
1603  return ary;
1604 }
1605 
1606 /*
1607  * call-seq:
1608  * hsh.has_key?(key) -> true or false
1609  * hsh.include?(key) -> true or false
1610  * hsh.key?(key) -> true or false
1611  * hsh.member?(key) -> true or false
1612  *
1613  * Returns <code>true</code> if the given key is present in <i>hsh</i>.
1614  *
1615  * h = { "a" => 100, "b" => 200 }
1616  * h.has_key?("a") #=> true
1617  * h.has_key?("z") #=> false
1618  *
1619  */
1620 
1621 static VALUE
1623 {
1624  if (!RHASH(hash)->ntbl)
1625  return Qfalse;
1626  if (st_lookup(RHASH(hash)->ntbl, key, 0)) {
1627  return Qtrue;
1628  }
1629  return Qfalse;
1630 }
1631 
1632 static int
1634 {
1635  VALUE *data = (VALUE *)arg;
1636 
1637  if (rb_equal(value, data[1])) {
1638  data[0] = Qtrue;
1639  return ST_STOP;
1640  }
1641  return ST_CONTINUE;
1642 }
1643 
1644 /*
1645  * call-seq:
1646  * hsh.has_value?(value) -> true or false
1647  * hsh.value?(value) -> true or false
1648  *
1649  * Returns <code>true</code> if the given value is present for some key
1650  * in <i>hsh</i>.
1651  *
1652  * h = { "a" => 100, "b" => 200 }
1653  * h.has_value?(100) #=> true
1654  * h.has_value?(999) #=> false
1655  */
1656 
1657 static VALUE
1659 {
1660  VALUE data[2];
1661 
1662  data[0] = Qfalse;
1663  data[1] = val;
1665  return data[0];
1666 }
1667 
1668 struct equal_data {
1671  int eql;
1672 };
1673 
1674 static int
1676 {
1677  struct equal_data *data = (struct equal_data *)arg;
1678  st_data_t val2;
1679 
1680  if (!st_lookup(data->tbl, key, &val2)) {
1681  data->result = Qfalse;
1682  return ST_STOP;
1683  }
1684  if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) {
1685  data->result = Qfalse;
1686  return ST_STOP;
1687  }
1688  return ST_CONTINUE;
1689 }
1690 
1691 static VALUE
1693 {
1694  struct equal_data *data;
1695 
1696  if (recur) return Qtrue; /* Subtle! */
1697  data = (struct equal_data*)dt;
1698  data->result = Qtrue;
1699  rb_hash_foreach(hash, eql_i, dt);
1700 
1701  return data->result;
1702 }
1703 
1704 static VALUE
1705 hash_equal(VALUE hash1, VALUE hash2, int eql)
1706 {
1707  struct equal_data data;
1708 
1709  if (hash1 == hash2) return Qtrue;
1710  if (!RB_TYPE_P(hash2, T_HASH)) {
1711  if (!rb_respond_to(hash2, rb_intern("to_hash"))) {
1712  return Qfalse;
1713  }
1714  if (eql)
1715  return rb_eql(hash2, hash1);
1716  else
1717  return rb_equal(hash2, hash1);
1718  }
1719  if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2))
1720  return Qfalse;
1721  if (!RHASH(hash1)->ntbl || !RHASH(hash2)->ntbl)
1722  return Qtrue;
1723  if (RHASH(hash1)->ntbl->type != RHASH(hash2)->ntbl->type)
1724  return Qfalse;
1725 #if 0
1726  if (!(rb_equal(RHASH_IFNONE(hash1), RHASH_IFNONE(hash2)) &&
1727  FL_TEST(hash1, HASH_PROC_DEFAULT) == FL_TEST(hash2, HASH_PROC_DEFAULT)))
1728  return Qfalse;
1729 #endif
1730 
1731  data.tbl = RHASH(hash2)->ntbl;
1732  data.eql = eql;
1733  return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
1734 }
1735 
1736 /*
1737  * call-seq:
1738  * hsh == other_hash -> true or false
1739  *
1740  * Equality---Two hashes are equal if they each contain the same number
1741  * of keys and if each key-value pair is equal to (according to
1742  * <code>Object#==</code>) the corresponding elements in the other
1743  * hash.
1744  *
1745  * h1 = { "a" => 1, "c" => 2 }
1746  * h2 = { 7 => 35, "c" => 2, "a" => 1 }
1747  * h3 = { "a" => 1, "c" => 2, 7 => 35 }
1748  * h4 = { "a" => 1, "d" => 2, "f" => 35 }
1749  * h1 == h2 #=> false
1750  * h2 == h3 #=> true
1751  * h3 == h4 #=> false
1752  *
1753  */
1754 
1755 static VALUE
1757 {
1758  return hash_equal(hash1, hash2, FALSE);
1759 }
1760 
1761 /*
1762  * call-seq:
1763  * hash.eql?(other) -> true or false
1764  *
1765  * Returns <code>true</code> if <i>hash</i> and <i>other</i> are
1766  * both hashes with the same content.
1767  */
1768 
1769 static VALUE
1770 rb_hash_eql(VALUE hash1, VALUE hash2)
1771 {
1772  return hash_equal(hash1, hash2, TRUE);
1773 }
1774 
1775 static int
1777 {
1778  st_index_t *hval = (st_index_t *)arg;
1779  st_index_t hdata[2];
1780 
1781  hdata[0] = rb_hash(key);
1782  hdata[1] = rb_hash(val);
1783  *hval ^= st_hash(hdata, sizeof(hdata), 0);
1784  return ST_CONTINUE;
1785 }
1786 
1787 static VALUE
1788 recursive_hash(VALUE hash, VALUE dummy, int recur)
1789 {
1790  st_index_t hval;
1791 
1792  if (!RHASH(hash)->ntbl)
1793  return LONG2FIX(0);
1794  hval = RHASH(hash)->ntbl->num_entries;
1795  if (!hval) return LONG2FIX(0);
1796  if (recur)
1797  hval = rb_hash_uint(rb_hash_start(rb_hash(rb_cHash)), hval);
1798  else
1799  rb_hash_foreach(hash, hash_i, (VALUE)&hval);
1800  hval = rb_hash_end(hval);
1801  return INT2FIX(hval);
1802 }
1803 
1804 /*
1805  * call-seq:
1806  * hsh.hash -> fixnum
1807  *
1808  * Compute a hash-code for this hash. Two hashes with the same content
1809  * will have the same hash code (and will compare using <code>eql?</code>).
1810  */
1811 
1812 static VALUE
1814 {
1815  return rb_exec_recursive_outer(recursive_hash, hash, 0);
1816 }
1817 
1818 static int
1820 {
1821  rb_hash_aset(hash, value, key);
1822  return ST_CONTINUE;
1823 }
1824 
1825 /*
1826  * call-seq:
1827  * hsh.invert -> new_hash
1828  *
1829  * Returns a new hash created by using <i>hsh</i>'s values as keys, and
1830  * the keys as values.
1831  *
1832  * h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
1833  * h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
1834  *
1835  */
1836 
1837 static VALUE
1839 {
1840  VALUE h = rb_hash_new();
1841 
1843  return h;
1844 }
1845 
1846 static int
1848 {
1849  *value = arg;
1850  return ST_CONTINUE;
1851 }
1852 
1854 
1855 static int
1856 rb_hash_update_i(VALUE key, VALUE value, VALUE hash)
1857 {
1858  RHASH_UPDATE(hash, key, rb_hash_update_callback, value);
1859  return ST_CONTINUE;
1860 }
1861 
1862 static int
1864 {
1865  VALUE newvalue = (VALUE)arg;
1866  if (existing) {
1867  newvalue = rb_yield_values(3, (VALUE)*key, (VALUE)*value, newvalue);
1868  }
1869  *value = (st_data_t)newvalue;
1870  return ST_CONTINUE;
1871 }
1872 
1874 
1875 static int
1876 rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash)
1877 {
1878  RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
1879  return ST_CONTINUE;
1880 }
1881 
1882 /*
1883  * call-seq:
1884  * hsh.merge!(other_hash) -> hsh
1885  * hsh.update(other_hash) -> hsh
1886  * hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh
1887  * hsh.update(other_hash){|key, oldval, newval| block} -> hsh
1888  *
1889  * Adds the contents of _other_hash_ to _hsh_. If no block is specified,
1890  * entries with duplicate keys are overwritten with the values from
1891  * _other_hash_, otherwise the value of each duplicate key is determined by
1892  * calling the block with the key, its value in _hsh_ and its value in
1893  * _other_hash_.
1894  *
1895  * h1 = { "a" => 100, "b" => 200 }
1896  * h2 = { "b" => 254, "c" => 300 }
1897  * h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
1898  *
1899  * h1 = { "a" => 100, "b" => 200 }
1900  * h2 = { "b" => 254, "c" => 300 }
1901  * h1.merge!(h2) { |key, v1, v2| v1 }
1902  * #=> {"a"=>100, "b"=>200, "c"=>300}
1903  */
1904 
1905 static VALUE
1907 {
1908  rb_hash_modify(hash1);
1909  hash2 = to_hash(hash2);
1910  if (rb_block_given_p()) {
1911  rb_hash_foreach(hash2, rb_hash_update_block_i, hash1);
1912  }
1913  else {
1914  rb_hash_foreach(hash2, rb_hash_update_i, hash1);
1915  }
1916  return hash1;
1917 }
1918 
1919 struct update_arg {
1923 };
1924 
1925 static int
1927 {
1928  struct update_arg *arg = (struct update_arg *)arg0;
1929  VALUE newvalue = arg->value;
1930  if (existing) {
1931  newvalue = (*arg->func)((VALUE)*key, (VALUE)*value, newvalue);
1932  }
1933  *value = (st_data_t)newvalue;
1934  return ST_CONTINUE;
1935 }
1936 
1938 
1939 static int
1940 rb_hash_update_func_i(VALUE key, VALUE value, VALUE arg0)
1941 {
1942  struct update_arg *arg = (struct update_arg *)arg0;
1943  VALUE hash = arg->hash;
1944 
1945  arg->value = value;
1946  RHASH_UPDATE(hash, key, rb_hash_update_func_callback, arg);
1947  return ST_CONTINUE;
1948 }
1949 
1950 VALUE
1952 {
1953  rb_hash_modify(hash1);
1954  hash2 = to_hash(hash2);
1955  if (func) {
1956  struct update_arg arg;
1957  arg.hash = hash1;
1958  arg.func = func;
1959  rb_hash_foreach(hash2, rb_hash_update_func_i, (VALUE)&arg);
1960  }
1961  else {
1962  rb_hash_foreach(hash2, rb_hash_update_i, hash1);
1963  }
1964  return hash1;
1965 }
1966 
1967 /*
1968  * call-seq:
1969  * hsh.merge(other_hash) -> new_hash
1970  * hsh.merge(other_hash){|key, oldval, newval| block} -> new_hash
1971  *
1972  * Returns a new hash containing the contents of <i>other_hash</i> and
1973  * the contents of <i>hsh</i>. If no block is specified, the value for
1974  * entries with duplicate keys will be that of <i>other_hash</i>. Otherwise
1975  * the value for each duplicate key is determined by calling the block
1976  * with the key, its value in <i>hsh</i> and its value in <i>other_hash</i>.
1977  *
1978  * h1 = { "a" => 100, "b" => 200 }
1979  * h2 = { "b" => 254, "c" => 300 }
1980  * h1.merge(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
1981  * h1.merge(h2){|key, oldval, newval| newval - oldval}
1982  * #=> {"a"=>100, "b"=>54, "c"=>300}
1983  * h1 #=> {"a"=>100, "b"=>200}
1984  *
1985  */
1986 
1987 static VALUE
1989 {
1990  return rb_hash_update(rb_obj_dup(hash1), hash2);
1991 }
1992 
1993 static int
1995 {
1996  VALUE *args = (VALUE *)arg;
1997 
1998  if (RTEST(rb_equal(args[0], key))) {
1999  args[1] = rb_assoc_new(key, val);
2000  return ST_STOP;
2001  }
2002  return ST_CONTINUE;
2003 }
2004 
2005 /*
2006  * call-seq:
2007  * hash.assoc(obj) -> an_array or nil
2008  *
2009  * Searches through the hash comparing _obj_ with the key using <code>==</code>.
2010  * Returns the key-value pair (two elements array) or +nil+
2011  * if no match is found. See <code>Array#assoc</code>.
2012  *
2013  * h = {"colors" => ["red", "blue", "green"],
2014  * "letters" => ["a", "b", "c" ]}
2015  * h.assoc("letters") #=> ["letters", ["a", "b", "c"]]
2016  * h.assoc("foo") #=> nil
2017  */
2018 
2019 VALUE
2021 {
2022  VALUE args[2];
2023 
2024  args[0] = obj;
2025  args[1] = Qnil;
2026  rb_hash_foreach(hash, assoc_i, (VALUE)args);
2027  return args[1];
2028 }
2029 
2030 static int
2032 {
2033  VALUE *args = (VALUE *)arg;
2034 
2035  if (RTEST(rb_equal(args[0], val))) {
2036  args[1] = rb_assoc_new(key, val);
2037  return ST_STOP;
2038  }
2039  return ST_CONTINUE;
2040 }
2041 
2042 /*
2043  * call-seq:
2044  * hash.rassoc(obj) -> an_array or nil
2045  *
2046  * Searches through the hash comparing _obj_ with the value using <code>==</code>.
2047  * Returns the first key-value pair (two-element array) that matches. See
2048  * also <code>Array#rassoc</code>.
2049  *
2050  * a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"}
2051  * a.rassoc("two") #=> [2, "two"]
2052  * a.rassoc("four") #=> nil
2053  */
2054 
2055 VALUE
2057 {
2058  VALUE args[2];
2059 
2060  args[0] = obj;
2061  args[1] = Qnil;
2062  rb_hash_foreach(hash, rassoc_i, (VALUE)args);
2063  return args[1];
2064 }
2065 
2066 /*
2067  * call-seq:
2068  * hash.flatten -> an_array
2069  * hash.flatten(level) -> an_array
2070  *
2071  * Returns a new array that is a one-dimensional flattening of this
2072  * hash. That is, for every key or value that is an array, extract
2073  * its elements into the new array. Unlike Array#flatten, this
2074  * method does not flatten recursively by default. The optional
2075  * <i>level</i> argument determines the level of recursion to flatten.
2076  *
2077  * a = {1=> "one", 2 => [2,"two"], 3 => "three"}
2078  * a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
2079  * a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
2080  */
2081 
2082 static VALUE
2084 {
2085  VALUE ary, tmp;
2086 
2087  ary = rb_hash_to_a(hash);
2088  if (argc == 0) {
2089  argc = 1;
2090  tmp = INT2FIX(1);
2091  argv = &tmp;
2092  }
2093  rb_funcall2(ary, rb_intern("flatten!"), argc, argv);
2094  return ary;
2095 }
2096 
2097 /*
2098  * call-seq:
2099  * hsh.compare_by_identity -> hsh
2100  *
2101  * Makes <i>hsh</i> compare its keys by their identity, i.e. it
2102  * will consider exact same objects as same keys.
2103  *
2104  * h1 = { "a" => 100, "b" => 200, :c => "c" }
2105  * h1["a"] #=> 100
2106  * h1.compare_by_identity
2107  * h1.compare_by_identity? #=> true
2108  * h1["a"] #=> nil # different objects.
2109  * h1[:c] #=> "c" # same symbols are all same.
2110  *
2111  */
2112 
2113 static VALUE
2115 {
2116  rb_hash_modify(hash);
2117  RHASH(hash)->ntbl->type = &identhash;
2118  rb_hash_rehash(hash);
2119  return hash;
2120 }
2121 
2122 /*
2123  * call-seq:
2124  * hsh.compare_by_identity? -> true or false
2125  *
2126  * Returns <code>true</code> if <i>hsh</i> will compare its keys by
2127  * their identity. Also see <code>Hash#compare_by_identity</code>.
2128  *
2129  */
2130 
2131 static VALUE
2133 {
2134  if (!RHASH(hash)->ntbl)
2135  return Qfalse;
2136  if (RHASH(hash)->ntbl->type == &identhash) {
2137  return Qtrue;
2138  }
2139  return Qfalse;
2140 }
2141 
2142 static int path_tainted = -1;
2143 
2144 static char **origenviron;
2145 #ifdef _WIN32
2146 #define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
2147 #define FREE_ENVIRON(e) rb_w32_free_environ(e)
2148 static char **my_environ;
2149 #undef environ
2150 #define environ my_environ
2151 #undef getenv
2152 #define getenv(n) rb_w32_ugetenv(n)
2153 #elif defined(__APPLE__)
2154 #undef environ
2155 #define environ (*_NSGetEnviron())
2156 #define GET_ENVIRON(e) (e)
2157 #define FREE_ENVIRON(e)
2158 #else
2159 extern char **environ;
2160 #define GET_ENVIRON(e) (e)
2161 #define FREE_ENVIRON(e)
2162 #endif
2163 #ifdef ENV_IGNORECASE
2164 #define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2165 #define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
2166 #else
2167 #define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2168 #define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
2169 #endif
2170 
2171 static VALUE
2172 env_str_new(const char *ptr, long len)
2173 {
2174 #ifdef _WIN32
2176 #else
2177  VALUE str = rb_locale_str_new(ptr, len);
2178 #endif
2179 
2180  rb_obj_freeze(str);
2181  return str;
2182 }
2183 
2184 static VALUE
2185 env_str_new2(const char *ptr)
2186 {
2187  if (!ptr) return Qnil;
2188  return env_str_new(ptr, strlen(ptr));
2189 }
2190 
2191 static VALUE
2193 {
2194  char *nam, *val;
2195 
2196  rb_secure(4);
2197  SafeStringValue(name);
2198  nam = RSTRING_PTR(name);
2199  if (memchr(nam, '\0', RSTRING_LEN(name))) {
2200  rb_raise(rb_eArgError, "bad environment variable name");
2201  }
2202  val = getenv(nam);
2203  if (val) {
2204  VALUE value = env_str_new2(val);
2205 
2206  ruby_setenv(nam, 0);
2207  if (ENVMATCH(nam, PATH_ENV)) {
2208  path_tainted = 0;
2209  }
2210  return value;
2211  }
2212  return Qnil;
2213 }
2214 
2215 /*
2216  * call-seq:
2217  * ENV.delete(name) -> value
2218  * ENV.delete(name) { |name| } -> value
2219  *
2220  * Deletes the environment variable with +name+ and returns the value of the
2221  * variable. If a block is given it will be called when the named environment
2222  * does not exist.
2223  */
2224 static VALUE
2226 {
2227  VALUE val;
2228 
2229  val = env_delete(obj, name);
2230  if (NIL_P(val) && rb_block_given_p()) rb_yield(name);
2231  return val;
2232 }
2233 
2234 static int env_path_tainted(const char *);
2235 
2236 /*
2237  * call-seq:
2238  * ENV[name] -> value
2239  *
2240  * Retrieves the +value+ for environment variable +name+ as a String. Returns
2241  * +nil+ if the named variable does not exist.
2242  */
2243 static VALUE
2245 {
2246  char *nam, *env;
2247 
2248  rb_secure(4);
2249  SafeStringValue(name);
2250  nam = RSTRING_PTR(name);
2251  if (memchr(nam, '\0', RSTRING_LEN(name))) {
2252  rb_raise(rb_eArgError, "bad environment variable name");
2253  }
2254  env = getenv(nam);
2255  if (env) {
2256  if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env)) {
2257 #ifdef _WIN32
2259 #else
2260  VALUE str = rb_filesystem_str_new_cstr(env);
2261 #endif
2262 
2263  rb_obj_freeze(str);
2264  return str;
2265  }
2266  return env_str_new2(env);
2267  }
2268  return Qnil;
2269 }
2270 
2271 /*
2272  * :yield: missing_name
2273  * call-seq:
2274  * ENV.fetch(name) -> value
2275  * ENV.fetch(name, default) -> value
2276  * ENV.fetch(name) { |missing_name| ... } -> value
2277  *
2278  * Retrieves the environment variable +name+.
2279  *
2280  * If the given name does not exist and neither +default+ nor a block a
2281  * provided an IndexError is raised. If a block is given it is called with
2282  * the missing name to provide a value. If a default value is given it will
2283  * be returned when no block is given.
2284  */
2285 static VALUE
2287 {
2288  VALUE key, if_none;
2289  long block_given;
2290  char *nam, *env;
2291 
2292  rb_secure(4);
2293  rb_scan_args(argc, argv, "11", &key, &if_none);
2294  block_given = rb_block_given_p();
2295  if (block_given && argc == 2) {
2296  rb_warn("block supersedes default value argument");
2297  }
2298  SafeStringValue(key);
2299  nam = RSTRING_PTR(key);
2300  if (memchr(nam, '\0', RSTRING_LEN(key))) {
2301  rb_raise(rb_eArgError, "bad environment variable name");
2302  }
2303  env = getenv(nam);
2304  if (!env) {
2305  if (block_given) return rb_yield(key);
2306  if (argc == 1) {
2307  rb_raise(rb_eKeyError, "key not found");
2308  }
2309  return if_none;
2310  }
2311  if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env))
2312 #ifdef _WIN32
2314 #else
2315  return rb_filesystem_str_new_cstr(env);
2316 #endif
2317  return env_str_new2(env);
2318 }
2319 
2320 static void
2321 path_tainted_p(const char *path)
2322 {
2323  path_tainted = rb_path_check(path)?0:1;
2324 }
2325 
2326 static int
2327 env_path_tainted(const char *path)
2328 {
2329  if (path_tainted < 0) {
2330  path_tainted_p(path);
2331  }
2332  return path_tainted;
2333 }
2334 
2335 int
2337 {
2338  if (path_tainted < 0) {
2340  }
2341  return path_tainted;
2342 }
2343 
2344 #if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
2345 #elif defined __sun
2346 static int
2347 in_origenv(const char *str)
2348 {
2349  char **env;
2350  for (env = origenviron; *env; ++env) {
2351  if (*env == str) return 1;
2352  }
2353  return 0;
2354 }
2355 #else
2356 static int
2357 envix(const char *nam)
2358 {
2359  register int i, len = strlen(nam);
2360  char **env;
2361 
2362  env = GET_ENVIRON(environ);
2363  for (i = 0; env[i]; i++) {
2364  if (ENVNMATCH(env[i],nam,len) && env[i][len] == '=')
2365  break; /* memcmp must come first to avoid */
2366  } /* potential SEGV's */
2367  FREE_ENVIRON(environ);
2368  return i;
2369 }
2370 #endif
2371 
2372 #if defined(_WIN32)
2373 static size_t
2374 getenvsize(const char* p)
2375 {
2376  const char* porg = p;
2377  while (*p++) p += strlen(p) + 1;
2378  return p - porg + 1;
2379 }
2380 static size_t
2381 getenvblocksize()
2382 {
2383  return (rb_w32_osver() >= 5) ? 32767 : 5120;
2384 }
2385 #endif
2386 
2387 void
2388 ruby_setenv(const char *name, const char *value)
2389 {
2390 #if defined(_WIN32)
2391  VALUE buf;
2392  int failed = 0;
2393  if (strchr(name, '=')) {
2394  fail:
2395  errno = EINVAL;
2396  rb_sys_fail("ruby_setenv");
2397  }
2398  if (value) {
2399  const char* p = GetEnvironmentStringsA();
2400  if (!p) goto fail; /* never happen */
2401  if (strlen(name) + 2 + strlen(value) + getenvsize(p) >= getenvblocksize()) {
2402  goto fail; /* 2 for '=' & '\0' */
2403  }
2404  buf = rb_sprintf("%s=%s", name, value);
2405  }
2406  else {
2407  buf = rb_sprintf("%s=", name);
2408  }
2409  failed = putenv(RSTRING_PTR(buf));
2410  /* even if putenv() failed, clean up and try to delete the
2411  * variable from the system area. */
2412  rb_str_resize(buf, 0);
2413  if (!value || !*value) {
2414  /* putenv() doesn't handle empty value */
2415  if (!SetEnvironmentVariable(name, value) &&
2416  GetLastError() != ERROR_ENVVAR_NOT_FOUND) goto fail;
2417  }
2418  if (failed) goto fail;
2419 #elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
2420 #undef setenv
2421 #undef unsetenv
2422  if (value) {
2423  if (setenv(name, value, 1))
2424  rb_sys_fail("setenv");
2425  } else {
2426 #ifdef VOID_UNSETENV
2427  unsetenv(name);
2428 #else
2429  if (unsetenv(name))
2430  rb_sys_fail("unsetenv");
2431 #endif
2432  }
2433 #elif defined __sun
2434  size_t len;
2435  char **env_ptr, *str;
2436  if (strchr(name, '=')) {
2437  errno = EINVAL;
2438  rb_sys_fail("ruby_setenv");
2439  }
2440  len = strlen(name);
2441  for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
2442  if (!strncmp(str, name, len) && str[len] == '=') {
2443  if (!in_origenv(str)) free(str);
2444  while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
2445  break;
2446  }
2447  }
2448  if (value) {
2449  str = malloc(len += strlen(value) + 2);
2450  snprintf(str, len, "%s=%s", name, value);
2451  if (putenv(str))
2452  rb_sys_fail("putenv");
2453  }
2454 #else /* WIN32 */
2455  size_t len;
2456  int i;
2457  if (strchr(name, '=')) {
2458  errno = EINVAL;
2459  rb_sys_fail("ruby_setenv");
2460  }
2461  i=envix(name); /* where does it go? */
2462 
2463  if (environ == origenviron) { /* need we copy environment? */
2464  int j;
2465  int max;
2466  char **tmpenv;
2467 
2468  for (max = i; environ[max]; max++) ;
2469  tmpenv = ALLOC_N(char*, max+2);
2470  for (j=0; j<max; j++) /* copy environment */
2471  tmpenv[j] = ruby_strdup(environ[j]);
2472  tmpenv[max] = 0;
2473  environ = tmpenv; /* tell exec where it is now */
2474  }
2475  if (environ[i]) {
2476  char **envp = origenviron;
2477  while (*envp && *envp != environ[i]) envp++;
2478  if (!*envp)
2479  xfree(environ[i]);
2480  if (!value) {
2481  while (environ[i]) {
2482  environ[i] = environ[i+1];
2483  i++;
2484  }
2485  return;
2486  }
2487  }
2488  else { /* does not exist yet */
2489  if (!value) return;
2490  REALLOC_N(environ, char*, i+2); /* just expand it a bit */
2491  environ[i+1] = 0; /* make sure it's null terminated */
2492  }
2493  len = strlen(name) + strlen(value) + 2;
2494  environ[i] = ALLOC_N(char, len);
2495  snprintf(environ[i],len,"%s=%s",name,value); /* all that work just for this */
2496 #endif /* WIN32 */
2497 }
2498 
2499 void
2500 ruby_unsetenv(const char *name)
2501 {
2502  ruby_setenv(name, 0);
2503 }
2504 
2505 /*
2506  * call-seq:
2507  * ENV[name] = value
2508  * ENV.store(name, value) -> value
2509  *
2510  * Sets the environment variable +name+ to +value+. If the value given is
2511  * +nil+ the environment variable is deleted.
2512  *
2513  */
2514 static VALUE
2516 {
2517  char *name, *value;
2518 
2519  if (rb_safe_level() >= 4) {
2520  rb_raise(rb_eSecurityError, "can't change environment variable");
2521  }
2522 
2523  if (NIL_P(val)) {
2524  env_delete(obj, nm);
2525  return Qnil;
2526  }
2527  StringValue(nm);
2528  StringValue(val);
2529  name = RSTRING_PTR(nm);
2530  value = RSTRING_PTR(val);
2531  if (memchr(name, '\0', RSTRING_LEN(nm)))
2532  rb_raise(rb_eArgError, "bad environment variable name");
2533  if (memchr(value, '\0', RSTRING_LEN(val)))
2534  rb_raise(rb_eArgError, "bad environment variable value");
2535 
2536  ruby_setenv(name, value);
2537  if (ENVMATCH(name, PATH_ENV)) {
2538  if (OBJ_TAINTED(val)) {
2539  /* already tainted, no check */
2540  path_tainted = 1;
2541  return val;
2542  }
2543  else {
2544  path_tainted_p(value);
2545  }
2546  }
2547  return val;
2548 }
2549 
2550 /*
2551  * call-seq:
2552  * ENV.keys -> Array
2553  *
2554  * Returns every environment variable name in an Array
2555  */
2556 static VALUE
2558 {
2559  char **env;
2560  VALUE ary;
2561 
2562  rb_secure(4);
2563  ary = rb_ary_new();
2564  env = GET_ENVIRON(environ);
2565  while (*env) {
2566  char *s = strchr(*env, '=');
2567  if (s) {
2568  rb_ary_push(ary, env_str_new(*env, s-*env));
2569  }
2570  env++;
2571  }
2572  FREE_ENVIRON(environ);
2573  return ary;
2574 }
2575 
2576 static VALUE
2578 {
2579  char **env;
2580  long cnt = 0;
2581 
2582  rb_secure(4);
2583 
2584  env = GET_ENVIRON(environ);
2585  for (; *env ; ++env) {
2586  if (strchr(*env, '=')) {
2587  cnt++;
2588  }
2589  }
2590  FREE_ENVIRON(environ);
2591  return LONG2FIX(cnt);
2592 }
2593 
2594 /*
2595  * call-seq:
2596  * ENV.each_key { |name| } -> Hash
2597  * ENV.each_key -> Enumerator
2598  *
2599  * Yields each environment variable name.
2600  *
2601  * An Enumerator is returned if no block is given.
2602  */
2603 static VALUE
2605 {
2606  VALUE keys;
2607  long i;
2608 
2609  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2610  keys = env_keys(); /* rb_secure(4); */
2611  for (i=0; i<RARRAY_LEN(keys); i++) {
2612  rb_yield(RARRAY_PTR(keys)[i]);
2613  }
2614  return ehash;
2615 }
2616 
2617 /*
2618  * call-seq:
2619  * ENV.values -> Array
2620  *
2621  * Returns every environment variable value as an Array
2622  */
2623 static VALUE
2625 {
2626  VALUE ary;
2627  char **env;
2628 
2629  rb_secure(4);
2630  ary = rb_ary_new();
2631  env = GET_ENVIRON(environ);
2632  while (*env) {
2633  char *s = strchr(*env, '=');
2634  if (s) {
2635  rb_ary_push(ary, env_str_new2(s+1));
2636  }
2637  env++;
2638  }
2639  FREE_ENVIRON(environ);
2640  return ary;
2641 }
2642 
2643 /*
2644  * call-seq:
2645  * ENV.each_value { |value| } -> Hash
2646  * ENV.each_value -> Enumerator
2647  *
2648  * Yields each environment variable +value+.
2649  *
2650  * An Enumerator is returned if no block was given.
2651  */
2652 static VALUE
2654 {
2655  VALUE values;
2656  long i;
2657 
2658  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2659  values = env_values(); /* rb_secure(4); */
2660  for (i=0; i<RARRAY_LEN(values); i++) {
2661  rb_yield(RARRAY_PTR(values)[i]);
2662  }
2663  return ehash;
2664 }
2665 
2666 /*
2667  * call-seq:
2668  * ENV.each { |name, value| } -> Hash
2669  * ENV.each -> Enumerator
2670  * ENV.each_pair { |name, value| } -> Hash
2671  * ENV.each_pair -> Enumerator
2672  *
2673  * Yields each environment variable +name+ and +value+.
2674  *
2675  * If no block is given an Enumerator is returned.
2676  */
2677 static VALUE
2679 {
2680  char **env;
2681  VALUE ary;
2682  long i;
2683 
2684  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2685 
2686  rb_secure(4);
2687  ary = rb_ary_new();
2688  env = GET_ENVIRON(environ);
2689  while (*env) {
2690  char *s = strchr(*env, '=');
2691  if (s) {
2692  rb_ary_push(ary, env_str_new(*env, s-*env));
2693  rb_ary_push(ary, env_str_new2(s+1));
2694  }
2695  env++;
2696  }
2697  FREE_ENVIRON(environ);
2698 
2699  for (i=0; i<RARRAY_LEN(ary); i+=2) {
2700  rb_yield(rb_assoc_new(RARRAY_PTR(ary)[i], RARRAY_PTR(ary)[i+1]));
2701  }
2702  return ehash;
2703 }
2704 
2705 /*
2706  * call-seq:
2707  * ENV.reject! { |name, value| } -> ENV or nil
2708  * ENV.reject! -> Enumerator
2709  *
2710  * Equivalent to ENV#delete_if but returns +nil+ if no changes were made.
2711  *
2712  * Returns an Enumerator if no block was given.
2713  */
2714 static VALUE
2716 {
2717  volatile VALUE keys;
2718  long i;
2719  int del = 0;
2720 
2721  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2722  keys = env_keys(); /* rb_secure(4); */
2723  RBASIC(keys)->klass = 0;
2724  for (i=0; i<RARRAY_LEN(keys); i++) {
2725  VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
2726  if (!NIL_P(val)) {
2727  if (RTEST(rb_yield_values(2, RARRAY_PTR(keys)[i], val))) {
2728  FL_UNSET(RARRAY_PTR(keys)[i], FL_TAINT);
2729  env_delete(Qnil, RARRAY_PTR(keys)[i]);
2730  del++;
2731  }
2732  }
2733  }
2734  if (del == 0) return Qnil;
2735  return envtbl;
2736 }
2737 
2738 /*
2739  * call-seq:
2740  * ENV.delete_if { |name, value| } -> Hash
2741  * ENV.delete_if -> Enumerator
2742  *
2743  * Deletes every environment variable for which the block evaluates to +true+.
2744  *
2745  * If no block is given an enumerator is returned instead.
2746  */
2747 static VALUE
2749 {
2750  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2751  env_reject_bang(ehash);
2752  return envtbl;
2753 }
2754 
2755 /*
2756  * call-seq:
2757  * ENV.values_at(name, ...) -> Array
2758  *
2759  * Returns an array containing the environment variable values associated with
2760  * the given names. See also ENV.select.
2761  */
2762 static VALUE
2764 {
2765  VALUE result;
2766  long i;
2767 
2768  rb_secure(4);
2769  result = rb_ary_new();
2770  for (i=0; i<argc; i++) {
2771  rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
2772  }
2773  return result;
2774 }
2775 
2776 /*
2777  * call-seq:
2778  * ENV.select { |name, value| } -> Hash
2779  * ENV.select -> Enumerator
2780  *
2781  * Returns a copy of the environment for entries where the block returns true.
2782  *
2783  * Returns an Enumerator if no block was given.
2784  */
2785 static VALUE
2787 {
2788  VALUE result;
2789  char **env;
2790 
2791  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2792  rb_secure(4);
2793  result = rb_hash_new();
2794  env = GET_ENVIRON(environ);
2795  while (*env) {
2796  char *s = strchr(*env, '=');
2797  if (s) {
2798  VALUE k = env_str_new(*env, s-*env);
2799  VALUE v = env_str_new2(s+1);
2800  if (RTEST(rb_yield_values(2, k, v))) {
2801  rb_hash_aset(result, k, v);
2802  }
2803  }
2804  env++;
2805  }
2806  FREE_ENVIRON(environ);
2807 
2808  return result;
2809 }
2810 
2811 /*
2812  * call-seq:
2813  * ENV.select! { |name, value| } -> ENV or nil
2814  * ENV.select! -> Enumerator
2815  *
2816  * Equivalent to ENV#keep_if but returns +nil+ if no changes were made.
2817  */
2818 static VALUE
2820 {
2821  volatile VALUE keys;
2822  long i;
2823  int del = 0;
2824 
2825  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2826  keys = env_keys(); /* rb_secure(4); */
2827  RBASIC(keys)->klass = 0;
2828  for (i=0; i<RARRAY_LEN(keys); i++) {
2829  VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
2830  if (!NIL_P(val)) {
2831  if (!RTEST(rb_yield_values(2, RARRAY_PTR(keys)[i], val))) {
2832  FL_UNSET(RARRAY_PTR(keys)[i], FL_TAINT);
2833  env_delete(Qnil, RARRAY_PTR(keys)[i]);
2834  del++;
2835  }
2836  }
2837  }
2838  if (del == 0) return Qnil;
2839  return envtbl;
2840 }
2841 
2842 /*
2843  * call-seq:
2844  * ENV.keep_if { |name, value| } -> Hash
2845  * ENV.keep_if -> Enumerator
2846  *
2847  * Deletes every environment variable where the block evaluates to +false+.
2848  *
2849  * Returns an enumerator if no block was given.
2850  */
2851 static VALUE
2853 {
2854  RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
2855  env_select_bang(ehash);
2856  return envtbl;
2857 }
2858 
2859 /*
2860  * call-seq:
2861  * ENV.clear
2862  *
2863  * Removes every environment variable.
2864  */
2865 VALUE
2867 {
2868  volatile VALUE keys;
2869  long i;
2870 
2871  keys = env_keys(); /* rb_secure(4); */
2872  for (i=0; i<RARRAY_LEN(keys); i++) {
2873  VALUE val = rb_f_getenv(Qnil, RARRAY_PTR(keys)[i]);
2874  if (!NIL_P(val)) {
2875  env_delete(Qnil, RARRAY_PTR(keys)[i]);
2876  }
2877  }
2878  return envtbl;
2879 }
2880 
2881 /*
2882  * call-seq:
2883  * ENV.to_s -> "ENV"
2884  *
2885  * Returns "ENV"
2886  */
2887 static VALUE
2889 {
2890  return rb_usascii_str_new2("ENV");
2891 }
2892 
2893 /*
2894  * call-seq:
2895  * ENV.inspect -> string
2896  *
2897  * Returns the contents of the environment as a String.
2898  */
2899 static VALUE
2901 {
2902  char **env;
2903  VALUE str, i;
2904 
2905  rb_secure(4);
2906  str = rb_str_buf_new2("{");
2907  env = GET_ENVIRON(environ);
2908  while (*env) {
2909  char *s = strchr(*env, '=');
2910 
2911  if (env != environ) {
2912  rb_str_buf_cat2(str, ", ");
2913  }
2914  if (s) {
2915  rb_str_buf_cat2(str, "\"");
2916  rb_str_buf_cat(str, *env, s-*env);
2917  rb_str_buf_cat2(str, "\"=>");
2918  i = rb_inspect(rb_str_new2(s+1));
2919  rb_str_buf_append(str, i);
2920  }
2921  env++;
2922  }
2923  FREE_ENVIRON(environ);
2924  rb_str_buf_cat2(str, "}");
2925  OBJ_TAINT(str);
2926 
2927  return str;
2928 }
2929 
2930 /*
2931  * call-seq:
2932  * ENV.to_a -> Array
2933  *
2934  * Converts the environment variables into an array of names and value arrays.
2935  *
2936  * ENV.to_a # => [["TERM", "xterm-color"], ["SHELL", "/bin/bash"], ...]
2937  *
2938  */
2939 static VALUE
2941 {
2942  char **env;
2943  VALUE ary;
2944 
2945  rb_secure(4);
2946  ary = rb_ary_new();
2947  env = GET_ENVIRON(environ);
2948  while (*env) {
2949  char *s = strchr(*env, '=');
2950  if (s) {
2951  rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
2952  env_str_new2(s+1)));
2953  }
2954  env++;
2955  }
2956  FREE_ENVIRON(environ);
2957  return ary;
2958 }
2959 
2960 /*
2961  * call-seq:
2962  * ENV.rehash
2963  *
2964  * Re-hashing the environment variables does nothing. It is provided for
2965  * compatibility with Hash.
2966  */
2967 static VALUE
2969 {
2970  return Qnil;
2971 }
2972 
2973 /*
2974  * call-seq:
2975  * ENV.length
2976  * ENV.size
2977  *
2978  * Returns the number of environment variables.
2979  */
2980 static VALUE
2982 {
2983  int i;
2984  char **env;
2985 
2986  rb_secure(4);
2987  env = GET_ENVIRON(environ);
2988  for (i=0; env[i]; i++)
2989  ;
2990  FREE_ENVIRON(environ);
2991  return INT2FIX(i);
2992 }
2993 
2994 /*
2995  * call-seq:
2996  * ENV.empty? -> true or false
2997  *
2998  * Returns true when there are no environment variables
2999  */
3000 static VALUE
3002 {
3003  char **env;
3004 
3005  rb_secure(4);
3006  env = GET_ENVIRON(environ);
3007  if (env[0] == 0) {
3008  FREE_ENVIRON(environ);
3009  return Qtrue;
3010  }
3011  FREE_ENVIRON(environ);
3012  return Qfalse;
3013 }
3014 
3015 /*
3016  * call-seq:
3017  * ENV.key?(name) -> true or false
3018  * ENV.include?(name) -> true or false
3019  * ENV.has_key?(name) -> true or false
3020  * ENV.member?(name) -> true or false
3021  *
3022  * Returns +true+ if there is an environment variable with the given +name+.
3023  */
3024 static VALUE
3026 {
3027  char *s;
3028 
3029  rb_secure(4);
3030  s = StringValuePtr(key);
3031  if (memchr(s, '\0', RSTRING_LEN(key)))
3032  rb_raise(rb_eArgError, "bad environment variable name");
3033  if (getenv(s)) return Qtrue;
3034  return Qfalse;
3035 }
3036 
3037 /*
3038  * call-seq:
3039  * ENV.assoc(name) -> Array or nil
3040  *
3041  * Returns an Array of the name and value of the environment variable with
3042  * +name+ or +nil+ if the name cannot be found.
3043  */
3044 static VALUE
3046 {
3047  char *s, *e;
3048 
3049  rb_secure(4);
3050  s = StringValuePtr(key);
3051  if (memchr(s, '\0', RSTRING_LEN(key)))
3052  rb_raise(rb_eArgError, "bad environment variable name");
3053  e = getenv(s);
3054  if (e) return rb_assoc_new(key, rb_tainted_str_new2(e));
3055  return Qnil;
3056 }
3057 
3058 /*
3059  * call-seq:
3060  * ENV.value?(value) -> true or false
3061  * ENV.has_value?(value) -> true or false
3062  *
3063  * Returns +true+ if there is an environment variable with the given +value+.
3064  */
3065 static VALUE
3067 {
3068  char **env;
3069 
3070  rb_secure(4);
3071  obj = rb_check_string_type(obj);
3072  if (NIL_P(obj)) return Qnil;
3073  env = GET_ENVIRON(environ);
3074  while (*env) {
3075  char *s = strchr(*env, '=');
3076  if (s++) {
3077  long len = strlen(s);
3078  if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
3079  FREE_ENVIRON(environ);
3080  return Qtrue;
3081  }
3082  }
3083  env++;
3084  }
3085  FREE_ENVIRON(environ);
3086  return Qfalse;
3087 }
3088 
3089 /*
3090  * call-seq:
3091  * ENV.rassoc(value)
3092  *
3093  * Returns an Array of the name and value of the environment variable with
3094  * +value+ or +nil+ if the value cannot be found.
3095  */
3096 static VALUE
3098 {
3099  char **env;
3100 
3101  rb_secure(4);
3102  obj = rb_check_string_type(obj);
3103  if (NIL_P(obj)) return Qnil;
3104  env = GET_ENVIRON(environ);
3105  while (*env) {
3106  char *s = strchr(*env, '=');
3107  if (s++) {
3108  long len = strlen(s);
3109  if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
3110  VALUE result = rb_assoc_new(rb_tainted_str_new(*env, s-*env-1), obj);
3111  FREE_ENVIRON(environ);
3112  return result;
3113  }
3114  }
3115  env++;
3116  }
3117  FREE_ENVIRON(environ);
3118  return Qnil;
3119 }
3120 
3121 /*
3122  * call-seq:
3123  * ENV.key(value) -> name
3124  *
3125  * Returns the name of the environment variable with +value+. If the value is
3126  * not found +nil+ is returned.
3127  */
3128 static VALUE
3130 {
3131  char **env;
3132  VALUE str;
3133 
3134  rb_secure(4);
3135  StringValue(value);
3136  env = GET_ENVIRON(environ);
3137  while (*env) {
3138  char *s = strchr(*env, '=');
3139  if (s++) {
3140  long len = strlen(s);
3141  if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) {
3142  str = env_str_new(*env, s-*env-1);
3143  FREE_ENVIRON(environ);
3144  return str;
3145  }
3146  }
3147  env++;
3148  }
3149  FREE_ENVIRON(environ);
3150  return Qnil;
3151 }
3152 
3153 /*
3154  * call-seq:
3155  * ENV.index(value) -> key
3156  *
3157  * Deprecated method that is equivalent to ENV.key
3158  */
3159 static VALUE
3161 {
3162  rb_warn("ENV.index is deprecated; use ENV.key");
3163  return env_key(dmy, value);
3164 }
3165 
3166 /*
3167  * call-seq:
3168  * ENV.to_hash -> hash
3169  * ENV.to_h -> hash
3170  *
3171  * Creates a hash with a copy of the environment variables.
3172  *
3173  */
3174 static VALUE
3176 {
3177  char **env;
3178  VALUE hash;
3179 
3180  rb_secure(4);
3181  hash = rb_hash_new();
3182  env = GET_ENVIRON(environ);
3183  while (*env) {
3184  char *s = strchr(*env, '=');
3185  if (s) {
3186  rb_hash_aset(hash, env_str_new(*env, s-*env),
3187  env_str_new2(s+1));
3188  }
3189  env++;
3190  }
3191  FREE_ENVIRON(environ);
3192  return hash;
3193 }
3194 
3195 /*
3196  * call-seq:
3197  * ENV.reject { |name, value| } -> Hash
3198  * ENV.reject -> Enumerator
3199  *
3200  * Same as ENV#delete_if, but works on (and returns) a copy of the
3201  * environment.
3202  */
3203 static VALUE
3205 {
3206  return rb_hash_delete_if(env_to_hash());
3207 }
3208 
3209 /*
3210  * call-seq:
3211  * ENV.shift -> Array or nil
3212  *
3213  * Removes an environment variable name-value pair from ENV and returns it as
3214  * an Array. Returns +nil+ if when the environment is empty.
3215  */
3216 static VALUE
3218 {
3219  char **env;
3220 
3221  rb_secure(4);
3222  env = GET_ENVIRON(environ);
3223  if (*env) {
3224  char *s = strchr(*env, '=');
3225  if (s) {
3226  VALUE key = env_str_new(*env, s-*env);
3228  env_delete(Qnil, key);
3229  return rb_assoc_new(key, val);
3230  }
3231  }
3232  FREE_ENVIRON(environ);
3233  return Qnil;
3234 }
3235 
3236 /*
3237  * call-seq:
3238  * ENV.invert -> Hash
3239  *
3240  * Returns a new hash created by using environment variable names as values
3241  * and values as names.
3242  */
3243 static VALUE
3245 {
3246  return rb_hash_invert(env_to_hash());
3247 }
3248 
3249 static int
3251 {
3252  env_aset(Qnil, key, val);
3253  if (rb_ary_includes(keys, key)) {
3254  rb_ary_delete(keys, key);
3255  }
3256  return ST_CONTINUE;
3257 }
3258 
3259 /*
3260  * call-seq:
3261  * ENV.replace(hash) -> env
3262  *
3263  * Replaces the contents of the environment variables with the contents of
3264  * +hash+.
3265  */
3266 static VALUE
3268 {
3269  volatile VALUE keys;
3270  long i;
3271 
3272  keys = env_keys(); /* rb_secure(4); */
3273  if (env == hash) return env;
3274  hash = to_hash(hash);
3275  rb_hash_foreach(hash, env_replace_i, keys);
3276 
3277  for (i=0; i<RARRAY_LEN(keys); i++) {
3278  env_delete(env, RARRAY_PTR(keys)[i]);
3279  }
3280  return env;
3281 }
3282 
3283 static int
3285 {
3286  if (rb_block_given_p()) {
3287  val = rb_yield_values(3, key, rb_f_getenv(Qnil, key), val);
3288  }
3289  env_aset(Qnil, key, val);
3290  return ST_CONTINUE;
3291 }
3292 
3293 /*
3294  * call-seq:
3295  * ENV.update(hash) -> Hash
3296  * ENV.update(hash) { |name, old_value, new_value| } -> Hash
3297  *
3298  * Adds the contents of +hash+ to the environment variables. If no block is
3299  * specified entries with duplicate keys are overwritten, otherwise the value
3300  * of each duplicate name is determined by calling the block with the key, its
3301  * value from the environment and its value from the hash.
3302  */
3303 static VALUE
3305 {
3306  rb_secure(4);
3307  if (env == hash) return env;
3308  hash = to_hash(hash);
3309  rb_hash_foreach(hash, env_update_i, 0);
3310  return env;
3311 }
3312 
3313 /*
3314  * A Hash is a dictionary-like collection of unique keys and their values.
3315  * Also called associative arrays, they are similar to Arrays, but where an
3316  * Array uses integers as its index, a Hash allows you to use any object
3317  * type.
3318  *
3319  * Hashes enumerate their values in the order that the corresponding keys
3320  * were inserted.
3321  *
3322  * A Hash can be easily created by using its implicit form:
3323  *
3324  * grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
3325  *
3326  * Hashes allow an alternate syntax form when your keys are always symbols.
3327  * Instead of
3328  *
3329  * options = { :font_size => 10, :font_family => "Arial" }
3330  *
3331  * You could write it as:
3332  *
3333  * options = { font_size: 10, font_family: "Arial" }
3334  *
3335  * Each named key is a symbol you can access in hash:
3336  *
3337  * options[:font_size] # => 10
3338  *
3339  * A Hash can also be created through its ::new method:
3340  *
3341  * grades = Hash.new
3342  * grades["Dorothy Doe"] = 9
3343  *
3344  * Hashes have a <em>default value</em> that is returned when accessing
3345  * keys that do not exist in the hash. If no default is set +nil+ is used.
3346  * You can set the default value by sending it as an argument to Hash.new:
3347  *
3348  * grades = Hash.new(0)
3349  *
3350  * Or by using the #default= method:
3351  *
3352  * grades = {"Timmy Doe" => 8}
3353  * grades.default = 0
3354  *
3355  * Accessing a value in a Hash requires using its key:
3356  *
3357  * puts grades["Jane Doe"] # => 10
3358  *
3359  * === Common Uses
3360  *
3361  * Hashes are an easy way to represent data structures, such as
3362  *
3363  * books = {}
3364  * books[:matz] = "The Ruby Language"
3365  * books[:black] = "The Well-Grounded Rubyist"
3366  *
3367  * Hashes are also commonly used as a way to have named parameters in
3368  * functions. Note that no brackets are used below. If a hash is the last
3369  * argument on a method call, no braces are needed, thus creating a really
3370  * clean interface:
3371  *
3372  * Person.create(name: "John Doe", age: 27)
3373  *
3374  * def self.create(params)
3375  * @name = params[:name]
3376  * @age = params[:age]
3377  * end
3378  *
3379  * === Hash Keys
3380  *
3381  * Two objects refer to the same hash key when their <code>hash</code> value
3382  * is identical and the two objects are <code>eql?</code> to each other.
3383  *
3384  * A user-defined class may be used as a hash key if the <code>hash</code>
3385  * and <code>eql?</code> methods are overridden to provide meaningful
3386  * behavior. By default, separate instances refer to separate hash keys.
3387  *
3388  * A typical implementation of <code>hash</code> is based on the
3389  * object's data while <code>eql?</code> is usually aliased to the overridden
3390  * <code>==</code> method:
3391  *
3392  * class Book
3393  * attr_reader :author, :title
3394  *
3395  * def initialize(author, title)
3396  * @author = author
3397  * @title = title
3398  * end
3399  *
3400  * def ==(other)
3401  * self.class === other and
3402  * other.author == @author and
3403  * other.title == @title
3404  * end
3405  *
3406  * alias eql? ==
3407  *
3408  * def hash
3409  * @author.hash ^ @title.hash # XOR
3410  * end
3411  * end
3412  *
3413  * book1 = Book.new 'matz', 'Ruby in a Nutshell'
3414  * book2 = Book.new 'matz', 'Ruby in a Nutshell'
3415  *
3416  * reviews = {}
3417  *
3418  * reviews[book1] = 'Great reference!'
3419  * reviews[book2] = 'Nice and compact!'
3420  *
3421  * reviews.length #=> 1
3422  *
3423  * See also Object#hash and Object#eql?
3424  */
3425 
3426 void
3428 {
3429 #undef rb_intern
3430 #define rb_intern(str) rb_intern_const(str)
3431 
3432  id_hash = rb_intern("hash");
3433  id_yield = rb_intern("yield");
3434  id_default = rb_intern("default");
3435 
3436  rb_cHash = rb_define_class("Hash", rb_cObject);
3437 
3439 
3443  rb_define_method(rb_cHash,"initialize", rb_hash_initialize, -1);
3444  rb_define_method(rb_cHash,"initialize_copy", rb_hash_initialize_copy, 1);
3446 
3447  rb_define_method(rb_cHash,"to_hash", rb_hash_to_hash, 0);
3450  rb_define_method(rb_cHash,"inspect", rb_hash_inspect, 0);
3451  rb_define_alias(rb_cHash, "to_s", "inspect");
3452 
3459  rb_define_method(rb_cHash,"store", rb_hash_aset, 2);
3460  rb_define_method(rb_cHash,"default", rb_hash_default, -1);
3462  rb_define_method(rb_cHash,"default_proc", rb_hash_default_proc, 0);
3463  rb_define_method(rb_cHash,"default_proc=", rb_hash_set_default_proc, 1);
3467  rb_define_method(rb_cHash,"length", rb_hash_size, 0);
3469 
3470  rb_define_method(rb_cHash,"each_value", rb_hash_each_value, 0);
3471  rb_define_method(rb_cHash,"each_key", rb_hash_each_key, 0);
3472  rb_define_method(rb_cHash,"each_pair", rb_hash_each_pair, 0);
3474 
3477  rb_define_method(rb_cHash,"values_at", rb_hash_values_at, -1);
3478 
3481  rb_define_method(rb_cHash,"delete_if", rb_hash_delete_if, 0);
3482  rb_define_method(rb_cHash,"keep_if", rb_hash_keep_if, 0);
3490  rb_define_method(rb_cHash,"replace", rb_hash_replace, 1);
3493  rb_define_method(rb_cHash, "assoc", rb_hash_assoc, 1);
3494  rb_define_method(rb_cHash, "rassoc", rb_hash_rassoc, 1);
3495  rb_define_method(rb_cHash, "flatten", rb_hash_flatten, -1);
3496 
3497  rb_define_method(rb_cHash,"include?", rb_hash_has_key, 1);
3498  rb_define_method(rb_cHash,"member?", rb_hash_has_key, 1);
3499  rb_define_method(rb_cHash,"has_key?", rb_hash_has_key, 1);
3500  rb_define_method(rb_cHash,"has_value?", rb_hash_has_value, 1);
3503 
3504  rb_define_method(rb_cHash,"compare_by_identity", rb_hash_compare_by_id, 0);
3505  rb_define_method(rb_cHash,"compare_by_identity?", rb_hash_compare_by_id_p, 0);
3506 
3507  /* Document-class: ENV
3508  *
3509  * ENV is a hash-like accessor for environment variables.
3510  */
3511 
3512  /*
3513  * Hack to get RDoc to regard ENV as a class:
3514  * envtbl = rb_define_class("ENV", rb_cObject);
3515  */
3516  origenviron = environ;
3519 
3562 
3563  /*
3564  * ENV is a Hash-like accessor for environment variables.
3565  *
3566  * See ENV (the class) for more details.
3567  */
3569 }
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1456
VALUE value
Definition: hash.c:1921
void rb_define_global_const(const char *, VALUE)
Definition: variable.c:2216
#define RHASH_UPDATE(hash, key, func, arg)
Definition: hash.c:302
static VALUE empty_hash_alloc(VALUE klass)
Definition: hash.c:224
const char * rb_builtin_class_name(VALUE x)
Definition: error.c:414
VALUE rb_hash(VALUE obj)
Definition: hash.c:66
static VALUE hash_foreach_call(VALUE arg)
Definition: hash.c:190
static int rb_hash_invert_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:1819
Definition: st.h:108
static int each_pair_i(VALUE key, VALUE value)
Definition: hash.c:1388
static int rb_hash_update_callback(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
Definition: hash.c:1847
#define FL_EXIVAR
Definition: ruby.h:1117
st_foreach_func * func
Definition: hash.c:116
#define RARRAY_LEN(a)
Definition: ruby.h:899
static VALUE env_each_value(VALUE ehash)
Definition: hash.c:2653
#define FALSE
Definition: nkf.h:174
void rb_enc_copy(VALUE obj1, VALUE obj2)
Definition: encoding.c:856
static VALUE rb_hash_each_value(VALUE hash)
Definition: hash.c:1347
static VALUE env_delete_m(VALUE obj, VALUE name)
Definition: hash.c:2225
size_t strlen(const char *)
rb_hash_update_func * func
Definition: hash.c:1922
static VALUE rb_hash_has_value(VALUE hash, VALUE val)
Definition: hash.c:1658
int i
Definition: win32ole.c:784
static int path_tainted
Definition: hash.c:2142
#define T_FIXNUM
Definition: ruby.h:497
Definition: st.h:77
#define FREE_ENVIRON(e)
Definition: hash.c:2161
static int hash_aset_str(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
Definition: hash.c:1161
Definition: st.h:108
static void path_tainted_p(const char *path)
Definition: hash.c:2321
static int replace_i(VALUE key, VALUE val, VALUE hash)
Definition: hash.c:1216
static ID id_yield
Definition: hash.c:44
VALUE rb_hash_dup(VALUE hash)
Definition: hash.c:240
static VALUE rb_hash_s_try_convert(VALUE, VALUE)
Definition: hash.c:478
static int envix(const char *nam)
Definition: hash.c:2357
VALUE rb_yield_values(int n,...)
Definition: vm_eval.c:945
static int max(int a, int b)
Definition: strftime.c:141
static ID id_hash
Definition: hash.c:44
static int keep_if_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:1066
static void no_new_key(void)
Definition: hash.c:283
static int rb_hash_update_block_callback(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
Definition: hash.c:1863
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1497
VALUE rb_exec_recursive_outer(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4875
static int env_update_i(VALUE key, VALUE val)
Definition: hash.c:3284
VALUE rb_eKeyError
Definition: error.c:514
static VALUE rb_hash_size(VALUE hash)
Definition: hash.c:1296
static VALUE rb_hash_empty_p(VALUE hash)
Definition: hash.c:1315
#define RHASH_ITER_LEV(h)
Definition: ruby.h:929
static VALUE env_invert(void)
Definition: hash.c:3244
#define FL_TAINT
Definition: ruby.h:1115
#define CLASS_OF(v)
Definition: ruby.h:448
static VALUE rb_hash_default(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:672
#define ENVMATCH(n1, n2)
Definition: hash.c:2167
#define Qtrue
Definition: ruby.h:434
int st_insert(st_table *, st_data_t, st_data_t)
static void rb_hash_modify(VALUE hash)
Definition: hash.c:275
VALUE rb_cHash
Definition: hash.c:41
static VALUE env_str_new2(const char *ptr)
Definition: hash.c:2185
st_index_t rb_hash_end(st_index_t)
VALUE rb_hash_select_bang(VALUE hash)
Definition: hash.c:1084
static int keys_i(VALUE key, VALUE value, VALUE ary)
Definition: hash.c:1547
static VALUE env_to_s(void)
Definition: hash.c:2888
int rb_env_path_tainted(void)
Definition: hash.c:2336
static VALUE env_keys(void)
Definition: hash.c:2557
VALUE val
Definition: hash.c:874
VALUE rb_eTypeError
Definition: error.c:511
static VALUE env_delete_if(VALUE ehash)
Definition: hash.c:2748
st_table * tbl
Definition: hash.c:1670
#define HASH_PROC_DEFAULT
Definition: hash.c:33
static VALUE env_shift(void)
Definition: hash.c:3217
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
static int eql_i(VALUE key, VALUE val1, VALUE arg)
Definition: hash.c:1675
VALUE rb_str_buf_new2(const char *)
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
void st_free_table(st_table *)
Definition: st.c:334
#define HASH_DELETED
Definition: hash.c:32
static VALUE env_each_pair(VALUE ehash)
Definition: hash.c:2678
static ID id_default
Definition: hash.c:44
VALUE rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func)
Definition: hash.c:1951
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
struct st_table * rb_hash_tbl(VALUE hash)
Definition: hash.c:266
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:771
static int assoc_i(VALUE key, VALUE val, VALUE arg)
Definition: hash.c:1994
VALUE rb_to_int(VALUE)
Definition: object.c:2431
#define Check_Type(v, t)
Definition: ruby.h:539
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1780
static VALUE rb_hash_shift(VALUE hash)
Definition: hash.c:912
static VALUE inspect_hash(VALUE hash, VALUE dummy, int recur)
Definition: hash.c:1477
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2368
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4852
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE rb_hash_equal(VALUE hash1, VALUE hash2)
Definition: hash.c:1756
#define T_HASH
Definition: ruby.h:493
VALUE rb_tainted_str_new2(const char *)
st_index_t rb_str_hash(VALUE)
Definition: string.c:2244
#define ENVNMATCH(s1, s2, n)
Definition: hash.c:2168
VALUE rb_eSecurityError
Definition: error.c:520
static int hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp)
Definition: hash.c:154
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:695
static int env_path_tainted(const char *)
Definition: hash.c:2327
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:582
static int values_i(VALUE key, VALUE value, VALUE ary)
Definition: hash.c:1577
st_data_t st_index_t
Definition: st.h:63
#define RHASH_UPDATE_ITER(hash, iter_lev, key, func, arg)
Definition: hash.c:298
static VALUE env_keep_if(VALUE ehash)
Definition: hash.c:2852
VALUE rb_hash_fetch(VALUE hash, VALUE key)
Definition: hash.c:645
static VALUE rb_hash_each_key(VALUE hash)
Definition: hash.c:1380
#define FIXNUM_P(f)
Definition: ruby.h:355
rb_encoding * rb_utf8_encoding(void)
Definition: encoding.c:1168
static int rb_hash_search_value(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:1633
char ** environ
Definition: missing-pips.c:6
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2105
static VALUE rb_hash_replace(VALUE hash, VALUE hash2)
Definition: hash.c:1260
VALUE result
Definition: hash.c:1669
static int shift_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:878
#define rb_intern(str)
#define NOINSERT_UPDATE_CALLBACK(func)
Definition: hash.c:288
#define OBJ_TAINTED(x)
Definition: ruby.h:1153
#define RHASH_IFNONE(h)
Definition: ruby.h:930
const char * rb_obj_classname(VALUE)
Definition: variable.c:396
static VALUE rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:617
#define RHASH_TBL(h)
Definition: ruby.h:928
static VALUE envtbl
Definition: hash.c:43
static VALUE env_rassoc(VALUE dmy, VALUE obj)
Definition: hash.c:3097
static VALUE env_each_key(VALUE ehash)
Definition: hash.c:2604
VALUE rb_str_buf_cat(VALUE, const char *, long)
Definition: string.c:1947
#define NEWOBJ_OF(obj, type, klass, flags)
Definition: ruby.h:683
Win32OLEIDispatch * p
Definition: win32ole.c:786
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
Definition: hash.c:200
int args
Definition: win32ole.c:785
VALUE rb_obj_dup(VALUE)
Definition: object.c:338
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1537
const struct st_hash_type st_hashtype_num
#define RHASH(obj)
Definition: ruby.h:1102
#define fail()
#define FL_UNTRUSTED
Definition: ruby.h:1116
int st_lookup(st_table *, st_data_t, st_data_t *)
static int rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:484
static VALUE env_key(VALUE dmy, VALUE value)
Definition: hash.c:3129
#define FL_TEST(x, f)
Definition: ruby.h:1146
void Init_Hash(void)
Definition: hash.c:3427
#define ALLOC_N(type, n)
Definition: ruby.h:1223
int rb_block_given_p(void)
Definition: eval.c:672
static VALUE rb_hash_reject(VALUE hash)
Definition: hash.c:1004
static VALUE env_assoc(VALUE env, VALUE key)
Definition: hash.c:3045
static VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc)
Definition: hash.c:752
VALUE rb_hash_reject_bang(VALUE hash)
Definition: hash.c:978
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1426
VALUE rb_eRuntimeError
Definition: error.c:510
VALUE rb_exec_recursive_paired(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE, VALUE)
Definition: thread.c:4863
static VALUE env_select_bang(VALUE ehash)
Definition: hash.c:2819
static VALUE rb_hash_initialize_copy(VALUE hash, VALUE hash2)
Definition: hash.c:1224
char * ruby_strdup(const char *)
Definition: util.c:454
static int hash_i(VALUE key, VALUE val, VALUE arg)
Definition: hash.c:1776
static VALUE rb_env_size(VALUE ehash)
Definition: hash.c:2577
VALUE rb_ary_new(void)
Definition: array.c:424
VALUE rb_str_buf_cat2(VALUE, const char *)
Definition: string.c:1957
static int each_value_i(VALUE key, VALUE value)
Definition: hash.c:1321
#define snprintf
Definition: subst.h:6
VALUE rb_locale_str_new(const char *, long)
Definition: string.c:596
#define NIL_P(v)
Definition: ruby.h:446
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:499
static VALUE rb_hash_eql(VALUE hash1, VALUE hash2)
Definition: hash.c:1770
static VALUE env_delete(VALUE obj, VALUE name)
Definition: hash.c:2192
int st_delete(st_table *, st_data_t *, st_data_t *)
rb_atomic_t cnt[RUBY_NSIG]
Definition: signal.c:432
VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value)
Definition: intern.h:479
int rb_foreach_func(VALUE, VALUE, VALUE)
Definition: hash.c:145
#define TYPE(x)
Definition: ruby.h:513
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Definition: string.c:563
int argc
Definition: ruby.c:130
static VALUE rb_hash_to_h(VALUE hash)
Definition: hash.c:1531
static VALUE env_reject(void)
Definition: hash.c:3204
#define Qfalse
Definition: ruby.h:433
static VALUE rb_hash_each_pair(VALUE hash)
Definition: hash.c:1417
static VALUE rb_hash_values(VALUE hash)
Definition: hash.c:1596
#define rb_sourcefile()
Definition: tcltklib.c:97
int eql
Definition: hash.c:1671
static const struct st_hash_type objhash
Definition: hash.c:104
#define T_BIGNUM
Definition: ruby.h:495
static int rb_any_cmp(VALUE a, VALUE b)
Definition: hash.c:47
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1721
#define identhash
Definition: hash.c:110
#define RBIGNUM_DIGITS(b)
Definition: ruby.h:1087
static VALUE rb_hash_to_hash(VALUE hash)
Definition: hash.c:1517
VALUE hash
Definition: hash.c:148
#define GET_ENVIRON(e)
Definition: hash.c:2160
static VALUE env_replace(VALUE env, VALUE hash)
Definition: hash.c:3267
VALUE rb_str_resize(VALUE, long)
Definition: string.c:1853
static VALUE rb_hash_set_default(VALUE hash, VALUE ifnone)
Definition: hash.c:706
int rb_str_hash_cmp(VALUE, VALUE)
Definition: string.c:2254
void st_foreach_safe(st_table *table, int(*func)(ANYARGS), st_data_t a)
Definition: hash.c:133
static void rb_hash_modify_check(VALUE hash)
Definition: hash.c:258
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
static VALUE rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
Definition: hash.c:390
static char ** origenviron
Definition: hash.c:2144
static int select_i(VALUE key, VALUE value, VALUE result)
Definition: hash.c:1033
#define RSTRING_LEN(str)
Definition: ruby.h:862
static VALUE rb_hash_index(VALUE hash, VALUE value)
Definition: hash.c:816
VALUE rb_yield(VALUE)
Definition: vm_eval.c:934
static VALUE env_index(VALUE dmy, VALUE value)
Definition: hash.c:3160
static VALUE rb_f_getenv(VALUE obj, VALUE name)
Definition: hash.c:2244
#define REALLOC_N(var, type, n)
Definition: ruby.h:1225
int errno
#define TRUE
Definition: nkf.h:175
#define T_DATA
Definition: ruby.h:500
static VALUE hash_foreach_ensure(VALUE hash)
Definition: hash.c:178
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:91
static VALUE env_inspect(void)
Definition: hash.c:2900
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:805
#define OBJ_UNTRUSTED(x)
Definition: ruby.h:1155
VALUE rb_mEnumerable
Definition: enum.c:20
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1270
static VALUE rb_hash_update(VALUE hash1, VALUE hash2)
Definition: hash.c:1906
VALUE rb_hash_delete(VALUE hash, VALUE key)
Definition: hash.c:859
static VALUE rb_hash_key(VALUE hash, VALUE value)
Definition: hash.c:802
int rb_eql(VALUE, VALUE)
Definition: object.c:67
VALUE rb_ary_delete(VALUE ary, VALUE item)
Definition: array.c:2760
VALUE rb_hash_assoc(VALUE hash, VALUE obj)
Definition: hash.c:2020
#define malloc
Definition: ripper.c:98
static VALUE env_has_key(VALUE env, VALUE key)
Definition: hash.c:3025
VALUE rb_hash_new(void)
Definition: hash.c:234
#define unsetenv(name, val)
Definition: util.h:65
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:461
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:545
unsigned long ID
Definition: ruby.h:105
#define PATH_ENV
Definition: defines.h:218
#define RHASH_SIZE(h)
Definition: ruby.h:931
#define Qnil
Definition: ruby.h:435
static VALUE rb_hash_compare_by_id_p(VALUE hash)
Definition: hash.c:2132
#define BUILTIN_TYPE(x)
Definition: ruby.h:510
#define OBJ_TAINT(x)
Definition: ruby.h:1154
unsigned long VALUE
Definition: ruby.h:104
rb_encoding * rb_locale_encoding(void)
Definition: encoding.c:1214
static VALUE result
Definition: nkf.c:40
VALUE rb_hash_clear(VALUE hash)
Definition: hash.c:1138
static VALUE env_values(void)
Definition: hash.c:2624
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
Definition: intern.h:215
static int clear_i(VALUE key, VALUE value, VALUE dummy)
Definition: hash.c:1121
#define RBASIC(obj)
Definition: ruby.h:1094
VALUE rb_hash_keep_if(VALUE hash)
Definition: hash.c:1111
static VALUE hash_alloc(VALUE klass)
Definition: hash.c:214
char * strchr(char *, char)
void rb_extend_object(VALUE obj, VALUE module)
Definition: eval.c:1234
#define setenv(name, val)
Definition: util.h:64
static VALUE env_aset(VALUE obj, VALUE nm, VALUE val)
Definition: hash.c:2515
static VALUE env_to_a(void)
Definition: hash.c:2940
char * getenv()
static VALUE hash_equal(VALUE hash1, VALUE hash2, int eql)
Definition: hash.c:1705
static VALUE rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:2083
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:804
VALUE rb_str_ellipsize(VALUE, long)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition: string.c:7709
VALUE rb_proc_lambda_p(VALUE)
Definition: proc.c:231
static int each_key_i(VALUE key, VALUE value)
Definition: hash.c:1355
rb_foreach_func * func
Definition: hash.c:149
VALUE key
Definition: hash.c:873
void ruby_unsetenv(const char *name)
Definition: hash.c:2500
void rb_sys_fail(const char *mesg)
Definition: error.c:1899
static VALUE env_select(VALUE ehash)
Definition: hash.c:2786
VALUE rb_hash_freeze(VALUE hash)
Definition: hash.c:36
void xfree(void *)
#define FL_UNSET(x, f)
Definition: ruby.h:1150
#define RUBY_DTRACE_HASH_CREATE_ENABLED()
Definition: probes.h:59
NORETURN(static void no_new_key(void))
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1564
st_index_t st_hash(const void *ptr, size_t len, st_index_t h)
Definition: st.c:1313
static VALUE env_none(void)
Definition: hash.c:2968
static VALUE env_size(void)
Definition: hash.c:2981
static int delete_if_i(VALUE key, VALUE value, VALUE hash)
Definition: hash.c:933
static VALUE rb_hash_keys(VALUE hash)
Definition: hash.c:1566
#define recur(fmt)
#define RSTRING_PTR(str)
Definition: ruby.h:866
VALUE rb_usascii_str_new2(const char *)
st_table * st_init_table_with_size(const struct st_hash_type *, st_index_t)
Definition: st.c:229
VALUE rb_equal(VALUE, VALUE)
Definition: object.c:56
static int inspect_i(VALUE key, VALUE value, VALUE str)
Definition: hash.c:1455
static VALUE rb_hash_compare_by_id(VALUE hash)
Definition: hash.c:2114
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
Definition: hash.c:571
static VALUE to_hash(VALUE hash)
Definition: hash.c:455
#define rb_check_arity(argc, min, max)
Definition: intern.h:277
#define INT2FIX(i)
Definition: ruby.h:241
static int hash_aset(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
Definition: hash.c:1154
int rb_sourceline(void)
Definition: vm.c:816
int rb_path_check(const char *path)
Definition: file.c:5229
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2383
VALUE rb_block_proc(void)
Definition: proc.c:479
st_data_t arg
Definition: hash.c:117
VALUE rb_str_buf_cat_ascii(VALUE, const char *)
Definition: string.c:2081
#define ANYARGS
Definition: defines.h:57
static VALUE env_to_hash(void)
Definition: hash.c:3175
static VALUE rb_hash_has_key(VALUE hash, VALUE key)
Definition: hash.c:1622
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:557
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:560
#define RARRAY_PTR(a)
Definition: ruby.h:904
static VALUE hash_default_value(VALUE hash, VALUE key)
Definition: hash.c:532
static VALUE env_values_at(int argc, VALUE *argv)
Definition: hash.c:2763
VALUE rb_check_string_type(VALUE)
Definition: string.c:1508
uint8_t key[16]
Definition: random.c:1370
VALUE rb_any_to_s(VALUE)
Definition: object.c:384
VALUE rb_ary_includes(VALUE ary, VALUE item)
Definition: array.c:3666
static int shift_i_safe(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:889
static st_index_t rb_any_hash(VALUE a)
Definition: hash.c:84
#define LONG2FIX(i)
Definition: ruby.h:242
#define RTEST(v)
Definition: ruby.h:445
#define T_STRING
Definition: ruby.h:490
#define OBJ_INFECT(x, s)
Definition: ruby.h:1157
int st_foreach_check(st_table *, int(*)(ANYARGS), st_data_t, st_data_t)
Definition: st.c:909
st_index_t rb_hash_uint(st_index_t, st_index_t)
static VALUE rb_hash_invert(VALUE hash)
Definition: hash.c:1838
rb_encoding * rb_filesystem_encoding(void)
Definition: encoding.c:1248
int rb_method_basic_definition_p(VALUE, ID)
Definition: vm_method.c:1498
v
Definition: win32ole.c:798
DWORD rb_w32_osver(void)
Definition: win32.c:269
static int foreach_safe_i(st_data_t key, st_data_t value, struct foreach_safe_arg *arg)
Definition: hash.c:121
static VALUE rb_hash_hash(VALUE hash)
Definition: hash.c:1813
static VALUE recursive_eql(VALUE hash, VALUE dt, int recur)
Definition: hash.c:1692
const struct st_hash_type * type
Definition: st.h:78
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
Definition: st.h:108
static int rassoc_i(VALUE key, VALUE val, VALUE arg)
Definition: hash.c:2031
#define SafeStringValue(v)
Definition: ruby.h:552
VALUE rb_ary_new2(long capa)
Definition: array.c:417
VALUE rb_filesystem_str_new_cstr(const char *)
Definition: string.c:614
st_table * tbl
Definition: hash.c:115
#define rb_safe_level()
Definition: tcltklib.c:94
static VALUE rb_hash_merge(VALUE hash1, VALUE hash2)
Definition: hash.c:1988
const char * name
Definition: nkf.c:208
#define FL_SET(x, f)
Definition: ruby.h:1149
void st_cleanup_safe(st_table *, st_data_t)
Definition: st.c:797
static int rb_hash_update_func_callback(st_data_t *key, st_data_t *value, st_data_t arg0, int existing)
Definition: hash.c:1926
static VALUE recursive_hash(VALUE hash, VALUE dummy, int recur)
Definition: hash.c:1788
unsigned long st_data_t
Definition: st.h:35
#define StringValuePtr(v)
Definition: ruby.h:547
VALUE rb_hash_values_at(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:1021
VALUE rb_str_new_frozen(VALUE)
Definition: string.c:713
VALUE rb_inspect(VALUE)
Definition: object.c:402
st_table * st_copy(st_table *)
Definition: st.c:658
VALUE arg
Definition: hash.c:150
static int key_i(VALUE key, VALUE value, VALUE arg)
Definition: hash.c:776
static VALUE rb_hash_rehash(VALUE hash)
Definition: hash.c:513
int st_foreach_func(st_data_t, st_data_t, st_data_t)
Definition: hash.c:112
VALUE rb_env_clear(void)
Definition: hash.c:2866
static int env_replace_i(VALUE key, VALUE val, VALUE keys)
Definition: hash.c:3250
void rb_secure(int)
Definition: safe.c:79
void st_clear(st_table *)
Definition: st.c:308
VALUE rb_hash_delete_if(VALUE hash)
Definition: hash.c:959
Definition: ruby.h:921
#define rb_check_frozen(obj)
Definition: intern.h:258
int rb_proc_arity(VALUE)
Definition: proc.c:702
static VALUE env_empty_p(void)
Definition: hash.c:3001
st_table * st_init_table(const struct st_hash_type *)
Definition: st.c:266
static int to_a_i(VALUE key, VALUE value, VALUE ary)
Definition: hash.c:1425
VALUE rb_obj_freeze(VALUE)
Definition: object.c:989
void rb_copy_generic_ivar(VALUE, VALUE)
Definition: variable.c:1047
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1143
VALUE rb_tainted_str_new(const char *, long)
#define RHASH_EMPTY_P(h)
Definition: ruby.h:932
#define SYMBOL_P(x)
Definition: ruby.h:362
#define env
static VALUE env_reject_bang(VALUE ehash)
Definition: hash.c:2715
static VALUE env_fetch(int argc, VALUE *argv)
Definition: hash.c:2286
#define FIX2LONG(x)
Definition: ruby.h:353
#define Qundef
Definition: ruby.h:436
VALUE rb_hash_aset(VALUE, VALUE, VALUE)
VALUE rb_hash_select(VALUE hash)
Definition: hash.c:1055
static VALUE env_has_value(VALUE dmy, VALUE obj)
Definition: hash.c:3066
static VALUE env_update(VALUE env, VALUE hash)
Definition: hash.c:3304
int st_delete_safe(st_table *, st_data_t *, st_data_t *, st_data_t)
static VALUE rb_hash_delete_key(VALUE hash, VALUE key)
Definition: hash.c:823
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
void ruby_setenv(const char *name, const char *value)
Definition: hash.c:2388
VALUE rb_str_new2(const char *)
void rb_warn(const char *fmt,...)
Definition: error.c:216
free(psz)
VALUE rb_eArgError
Definition: error.c:512
static void default_proc_arity_check(VALUE proc)
Definition: hash.c:306
VALUE hash
Definition: hash.c:1920
#define T_MASK
Definition: md5.c:131
st_index_t rb_hash_start(st_index_t)
Definition: random.c:1416
VALUE rb_hash_rassoc(VALUE hash, VALUE obj)
Definition: hash.c:2056
static VALUE rb_hash_to_a(VALUE hash)
Definition: hash.c:1443
char ** argv
Definition: ruby.c:131
static VALUE rb_hash_initialize(int argc, VALUE *argv, VALUE hash)
Definition: hash.c:352
static VALUE env_str_new(const char *ptr, long len)
Definition: hash.c:2172
static VALUE rb_hash_default_proc(VALUE hash)
Definition: hash.c:730
#define StringValue(v)
Definition: ruby.h:546
#define RUBY_DTRACE_HASH_CREATE(arg0, arg1, arg2)
Definition: probes.h:60
static VALUE rb_hash_inspect(VALUE hash)
Definition: hash.c:1502
VALUE rb_str_new(const char *, long)
Definition: string.c:425
VALUE rb_obj_class(VALUE)
Definition: object.c:194