在 Ruby 中为什么 nil.id 返回 4? [英] In Ruby why does nil.id return 4?

查看:35
本文介绍了在 Ruby 中为什么 nil.id 返回 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经困扰我很长时间了.

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

为什么 nil.id 是 4?(或者 nil.object_id 如果你想对弃用挑剔)

Why would nil.id be 4? (or nil.object_id if you want to be picky about deprecations)

推荐答案

这是因为 nil 是语言初始化时创建的对象,而该对象的 id 恰好始终为 4.

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

有关为什么 id 恰好是 4 的更多信息,请参阅 这篇 博文.

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

这篇关于在 Ruby 中为什么 nil.id 返回 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆