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