Discussion:
ObjectSpace#each_object example issue
Chris White
2011-07-29 19:16:55 UTC
Permalink
The documentation for ObjectSpace#each_object has the following sample code:

 *     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

In this case, c, with the value of 12345678987654321 is supposed to show for being a Bignum. However on 64 bit systems this isn't the case:

= 64bit MacOSX Ruby SVN HEAD =
irb(main):001:0> puts RUBY_VERSION + " " + RUBY_PLATFORM + " " + RUBY_PATCHLEVEL.to_s
1.9.2 x86_64-darwin10.8.0 290
=> nil
irb(main):002:0> 12345678987654321.class
=> Fixnum

This won't show because as explained, "Immediate objects (Fixnums, Symbols true, false, and nil) are never returned." One a 32 bit system, however, this isn't an issue:

= 32bit Ubuntu Ruby SVN HEAD
irb(main):001:0> puts RUBY_VERSION + " " + RUBY_PLATFORM + " " + RUBY_PATCHLEVEL.to_s
1.9.4 i686-linux -1
=> nil
irb(main):002:0> 12345678987654321.class
=> Bignum

I see two possible solutions to this:

1) Use a number that produces Bignum on both 32 an 64 bit systems:

irb(main):003:0> 12345678987654321094903903903.class
=> Bignum

2) Split out the explanation to show 32 and 64 bit side by side?

3) Just add a side not explaining the issue with 32 and 64 bit differences

Also there might be another potential fix if there is some constant that holds the min and max values Bignum is capable of, but that's more of a feature request than a change in the docs.

- Chris
Twitter: @cwgem
Roger Pack
2011-08-02 20:28:49 UTC
Permalink
I'd probably just add a little note explaining the different ("this is
for 32 bit systems, for 64 bit it would...")
But that's just me.
Feel free to formulate a patch to the core and submit it for consideration:
http://blog.steveklabnik.com/2011/05/10/contributing-to-ruby-s-documentation.html
Cheers!
-roger-
Post by Chris White
 *     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
= 64bit MacOSX Ruby SVN HEAD =
irb(main):001:0> puts RUBY_VERSION + " " + RUBY_PLATFORM + " " + RUBY_PATCHLEVEL.to_s
1.9.2 x86_64-darwin10.8.0 290
=> nil
irb(main):002:0> 12345678987654321.class
=> Fixnum
= 32bit Ubuntu Ruby SVN HEAD
irb(main):001:0> puts RUBY_VERSION + " " + RUBY_PLATFORM + " " + RUBY_PATCHLEVEL.to_s
1.9.4 i686-linux -1
=> nil
irb(main):002:0> 12345678987654321.class
=> Bignum
irb(main):003:0> 12345678987654321094903903903.class
=> Bignum
2) Split out the explanation to show 32 and 64 bit side by side?
3) Just add a side not explaining the issue with 32 and 64 bit differences
Also there might be another potential fix if there is some constant that holds the min and max values Bignum is capable of, but that's more of a feature request than a change in the docs.
- Chris
Loading...