S
Stephan Wehner
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
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