search

Home  >  Q&A  >  body text

在Ruby中为什么$a和@a返回的是nil?

不应该是报错么?

PHP中文网PHP中文网2804 days ago1237

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-25 09:05:55

    Uninitialized global variables and instance variables have default values ​​of nil.
    You can take a look at the documentation:
    http://www.rubyist.net/~slage...

    Before initialization, a global variable has the special value nil.

    http://www.rubyist.net/~slage...

    instance variables have the nil value until they are initialized.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-04-25 09:05:55

    Accessing an uninitialized variable returns nil, and nil can be regarded as an error.

    Why? Because it is convenient, it is so troublesome to report errors every time and deal with them. Also, you are an awesome programmer, capable of using programming languages ​​well, and will not screw up the program because of nil (๑•̀ㅂ•́)و✧, so I won’t report an error.

    reply
    0
  • Cancelreply