Stephan Wehner wrote: > We traced a bug in some Ruby code back to it being possible to add > instance variables to the nil object. > > # irb > irb(main):001:0> nil.instance_variable_set(:@okidoki, 'the value of > okidoki') > => "the value of okidoki" > irb(main):002:0> nil.inspect > => "nil" > irb(main):003:0> nil.instance_variables.each { |v| puts > nil.instance_variable_get(v) } > the value of okidoki > => ["@okidoki"] > > I find the nil object is different from others throughout the ruby > language since it is kind of global: it can be accessed anywhere in the > code, and there is only one instance. So I feel it is not wise to allow > this. > > Are there good reasons for this feature of setting/getting instance > variables of nil? Do other object-oriented languages allow this? Well you certainly have to take into account that NilClass is singleton, but in doing so there can be some limited uses cases. For instance I added a #status accessor so I could pass non-critical failure messages up a call chain. T.