为什么 ruby​​2.0 中 true 和 nil 的 object_id 发生了变化? [英] Why was the object_id for true and nil changed in ruby2.0?

查看:46
本文介绍了为什么 ruby​​2.0 中 true 和 nil 的 object_id 发生了变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某个时候遇到了这个 ruby​​ object_id 分配问题,然后阅读这篇很棒的文章,其中讨论了 VALUE 并解释了原因object_id 为 true、nil 和 false.当我发现关于 true 和 nil 的 object_id 的明显变化时,我一直在玩弄 ruby​​2.0 object_id.

I came across this ruby object_id allocation question sometime back and then read this awesome article which talks about VALUE and explains why object_id of true, nil and false the way they are. I have been toying with ruby2.0 object_id when I found the apparent change that has been made regarding object_id of true and nil.

forbidden:~$ ruby -v
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
forbidden:~$
forbidden:~$ irb
irb(main):001:0> true.object_id
=> 20
irb(main):002:0> false.object_id
=> 0
irb(main):003:0> nil.object_id
=> 8
irb(main):004:0> exit
forbidden:~$
forbidden:~$ rvm use 1.9.3
Using /home/forbidden/.rvm/gems/ruby-1.9.3-p392
forbidden:~$ ruby -v
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
forbidden:~$
forbidden:~$ irb
irb(main):001:0> true.object_id
=> 2
irb(main):002:0> false.object_id
=> 0
irb(main):003:0> nil.object_id
=> 4

tl;dr: true 和 nil 的值在 1.9.3 和 1.8.7 中分别为 2、4,但已更改为 20, ruby​​2.0.0 中的 8 - 即使 false 的 id 保持不变,即 0 并且 Fixnum 的 id 保持相同的旧 2n+1 模式.

此外,Fixnum 和 Bignum 的实现方式在 2.0.0 中仍然相同,因为上述文章中给出的示例也以与以前相同的方式运行:

Also, the way Fixnum and Bignum are implemented is still the same in 2.0.0 as the example given in the above mentioned article also runs just the same way it used to:

irb(main):001:0> 
irb(main):002:0* ((2**62)).class
=> Bignum
irb(main):003:0> ((2**62)-1).class
=> Fixnum
irb(main):004:0>

这个 object_id 变化背后的原因是什么?

What's the reason behind this object_id change?

为什么做出这样的改变?这将如何帮助开发者?

Why was this change made? How is this going to help developers?

推荐答案

查看 定义这些值的 Ruby 源 表明这与flonums"有关(另请参阅 在引入的地方提交).搜索flonum"后,在 Ruby 邮件列表上的消息 讨论了它.

A look at the Ruby source where these values are defined suggests that this has something to do with "flonums" (also see the commit where this was introduced). A search for "flonum" came up with a message on the Ruby mailing list discussing it.

这是一种通过对某些浮点值使用立即数来加速 64 位机器上的浮点计算的技术,类似于对整数使用 Fixnums.Flonums 的模式是 ...xxxx xx10(即最后两位是 10,其中 fixnums 的最后一位是 1).其他立即数的 object_id 已更改以适应此更改.

This is a technique for speeding up floating point calculations on 64 bit machines by using immediate values for some floating point vales, similar to using Fixnums for integers. The pattern for Flonums is ...xxxx xx10 (i.e. the last two bits are 10, where for fixnums the last bit is 1). The object_ids of other immediate values have been changed to accomodate this change.

您可以通过查看 Ruby 1.9.3 和 2.0.0 中浮点数的 object_ids 来看到这一变化.

You can see this change by looking at the object_ids of floats in Ruby 1.9.3 and 2.0.0.

在 1.9.3 中,具有相同值的不同浮点数是不同的对象:

In 1.9.3 different floats with the same value are different objects:

1.9.3p385 :001 > s = 10.234
 => 10.234 
1.9.3p385 :002 > t = 10.234
 => 10.234 
1.9.3p385 :003 > s.object_id
 => 2160496240 
1.9.3p385 :004 > t.object_id
 => 2160508080 

在 2.0.0 中它们是相同的:

In 2.0.0 they are the same:

2.0.0p0 :001 > s = 10.234
 => 10.234 
2.0.0p0 :002 > t = 10.234
 => 10.234 
2.0.0p0 :003 > s.object_id
 => 82118635605473626 
2.0.0p0 :004 > t.object_id
 => 82118635605473626 

这篇关于为什么 ruby​​2.0 中 true 和 nil 的 object_id 发生了变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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