> > nil.object_id
= > 4
原因是因为nil是一个固定的object,在如下在$rubyhome\lib\1.8\i386-mswin32中的ruby.h头文件里面有如下特殊对象的构造。
/* special contants - i.e. non-zero and non-fixnum constants */
#define Qfalse ((VALUE)0)
#define Qtrue ((VALUE)2)
#define Qnil ((VALUE)4)
#define Qundef ((VALUE)6) /* undefined value for placeholder */
在rails的activesupport里面,也有一个whiny_nil.rb的类有些相关的说明。
Ruby Nil 对象ID解析
本文探讨了 Ruby 中 nil 对象的固定 ID (4) 的由来,详细介绍了其在 ruby.h 头文件中的定义,并提及了 Rails 的 activesupport 库中与之相关的 whiny_nil.rb 类。

被折叠的 条评论
为什么被折叠?



