Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
Does the set of all sets contain itself?


$ irb -r set
1.9.3p125 :001 > set = ObjectSpace.each_object(Set).to_set
 => #<Set: {#<Set: {...}>}>
1.9.3p125 :002 > set.include?(set)
 => false

However, the Enumeration of all Enumerations does contain itself.

1.9.3p125 :001 > e = ObjectSpace.each_object(Enumerator)
 => #<Enumerator: ObjectSpace:each_object(Enumerator)>
1.9.3p125 :002 > e.include?(e)
 => true

Why this is so is left as an exercise to the reader.

  1. Brennan says:

    After absolutely no research, I’m going to say that #to_set has something to do with it. Maybe #each_object returns a persistent reference to its results, and #to_set makes a copy, breaking the persistence. Otherwise #each_object returns an Enumerator and detects itself in the process.

  2. Dan Kubb says:

    A guess off the top of my head: At the point when ObjectSpace.each_object is evaluated the Set object returned by #to_set does not exist, so it doesn’t include itself.

    In the case of the Enumerator the actual process of finding all the objects in the system is lazily deferred until you start iterating over it. By that point the Enumerator object exists, and so it finds itself.

  3. MSN says:

    Isn’t this like saying if the universe contains itself?

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *

Share This