# Nil can have instance variables assigned

**URL:** <https://www.ruby-forum.com/t/nil-can-have-instance-variables-assigned/66747>\
**Category:** Ruby\
**Created:** [August 28, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/nil-can-have-instance-variables-assigned/66747 "2006-08-28T23:22:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [August 28, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/nil-can-have-instance-variables-assigned/66747/1 "2006-08-28T23:22:22Z")

</div>

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?

Thanks,

Stephan

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [August 28, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/nil-can-have-instance-variables-assigned/66747/2 "2006-08-28T23:52:56Z")

</div>

On Aug 28, 2006, at 5:22 PM, Stephan W. wrote:

> 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.

I think the uniformity of the Ruby object model is it’s strength. No  
need for lots of special rules about things that are objects and things  
that are not really objects and things that are objects but behave a  
little differently.

You can have instance variables associated with nil, true, false,  
instances of Fixnum, classes, modules, and so on. The language doesn’t  
care.

I’m not sure how the scoping of the literal ‘nil’ is related to the  
issue  
of instance variables. The literal ‘3’ would be considered global in  
that  
case also, for example. All the top level class objects are also  
‘global’.

Here is something interesting. I figured that you couldn’t reference  
the  
singleton class of nil, but it turns out that the singleton class of nil  
is NilClass, which makes a lot of sense since there can be one and only  
one instance of NilClass, that being the object most commonly known  
as nil!

a = class \<\<nil; self; end  
p a.class # NilClass

Same thing for true and false, which are also singleton instances (in  
the  
design pattern sense) of their respective classes (TrueClass,  
FalseClass).

Gary W.

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [August 29, 2006, 12:00am UTC](https://www.ruby-forum.com/t/nil-can-have-instance-variables-assigned/66747/3 "2006-08-29T00:00:58Z")

</div>

Stephan W. wrote:

> 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.
