ObjectSpace.each_object([module]) {|obj| ... } → fixnum
Calls the block once for each living, nonimmediate object in this Ruby process. If module is specified, calls the block for only those classes or modules that match (or are a subclass of) module. Returns the number of objects found. Immediate objects (Fixnums, Symbols true, false, and nil) are never returned. In the example below, each_object returns both the numbers we defined and several constants defined in the Math module.
a = 102.7 b = 95 # Won't be returned c = 12345678987654321 count = ObjectSpace.each_object(Numeric) {|x| p x } puts "Total count: #{count}"
produces:
12345678987654321 102.7 2.71828182845905 3.14159265358979 2.22044604925031e-16 1.7976931348623157e+308 2.2250738585072e-308 Total count: 7
Source Code
/* * call-seq: * ObjectSpace.each_object([module]) {|obj| ... } => fixnum * * Calls the block once for each living, nonimmediate object in this * Ruby process. If <i>module</i> is specified, calls the block * for only those classes or modules that match (or are a subclass of) * <i>module</i>. Returns the number of objects found. Immediate * objects (<code>Fixnum</code>s, <code>Symbol</code>s * <code>true</code>, <code>false</code>, and <code>nil</code>) are * never returned. In the example below, <code>each_object</code> * returns both the numbers we defined and several constants defined in * the <code>Math</code> module. * * a = 102.7 * b = 95 # Won't be returned * c = 12345678987654321 * count = ObjectSpace.each_object(Numeric) {|x| p x } * puts "Total count: #{count}" * * <em>produces:</em> * * 12345678987654321 * 102.7 * 2.71828182845905 * 3.14159265358979 * 2.22044604925031e-16 * 1.7976931348623157e+308 * 2.2250738585072e-308 * Total count: 7 * */ static VALUE os_each_obj(argc, argv) int argc; VALUE *argv; { VALUE of; rb_secure(4); if (rb_scan_args(argc, argv, "01", &of) == 0) { return os_live_obj(); } else { return os_obj_of(of); } }
<code/>and<pre/>for code samples.