In Ruby why does nil.id return 4?

后端 未结 4 592
陌清茗
陌清茗 2020-12-10 10:30

This is has been bugging me for quite some time.

>> nil.id
(irb):2: warning: Object#id will be deprecated; use Object#object_id
=> 4
4条回答
  •  我在风中等你
    2020-12-10 10:46

    This is because nil is an object created when the language initializes, and that object's id happens to always be 4.

    For more information about why the id happens to be 4, see this blog post.

提交回复
热议问题