[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

nil can have instance variables assigned.

Stephan Wehner

8/28/2006 9:22:00 PM

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

--
Posted via http://www.ruby-....

2 Answers

Gary Wright

8/28/2006 9:53:00 PM

0


On Aug 28, 2006, at 5:22 PM, Stephan Wehner 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 Wright




Trans

8/28/2006 9:59:00 PM

0


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.