Id2sym&安培; symbol.object_id [英] Id2sym & symbol.object_id

查看:172
本文介绍了Id2sym&安培; symbol.object_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用红宝石黑客导向网站,我发现及长整数LT;< 8 | 1 是OBJECT_ID任何的长整数。结果
我已经使用带符号类似的方法尝试。

Using ruby-hacking-guide site, I've found that fixnum << 8 | 1 is object_id of any fixnum.
I've tried using similar approach with symbol.

的#define ID2SYM(X)((VALUE)(((长)(X))&LT;&LT; 8 | SYMBOL_FLAG))

当左移8位, X 成为256的倍数,这意味着
  4.多个然后用按位或后(在此情况下,它的
  与加入)​​与 0×0E (十进制14)

When shifting 8 bits left, x becomes a multiple of 256, that means a multiple of 4. Then after with a bitwise or (in this case it’s the same as adding) with 0×0e (14 in decimal)

我与试了一下:一个:a.object_id = 175_976,在我的32位系统):

I have tried it with :a(:a.object_id = 175_976, on my 32-bit system):


  1. 的ASCII号码是97。

  2. 97 LT;&LT; 8 = 24832

  3. 24832 | 14 = 24_846

因此​​,它甚至还没有接近:一个的对象ID

So it's not even close to :a's object id.

我检查的 OBJECT_ID 来源,发现这一点:

I've checked source of object_id and found this:

  *  sizeof(RVALUE) is
  *  20 if 32-bit, double is 4-byte aligned
  *  24 if 32-bit, double is 8-byte aligned
  *  40 if 64-bit
  */
 if (SYMBOL_P(obj)) {
     return (SYM2ID(obj) * sizeof(RVALUE) + (4 << 2)) | FIXNUM_FLAG;

我有500〜000人,这是坏的价值。

I got ~ 500 000, which is bad value.

那么,什么我失踪?如何计算符号的object_id?

So what I'm missing? How to calculate object_id of symbol?

推荐答案

这是你从一个符号计算的 ID OBJECT_ID 不直接重新present该符号的字符串内容。它是一个索引到该红宝石保持包含字符串的表格。当您在Ruby中使用符号那么如果该符号没有被之前的当前进程中,它将被赋予 ID 符号表的下一个空闲插槽的价值

The ID value that you calculate from a symbols object_id doesn’t directly represent the string content of that symbol. It is an index into a table that Ruby maintains containing the string. When you use a symbol in Ruby then if that symbol hasn’t been used before in the current process it will be given the ID value of the next free slot in the symbol table.

这意味着,给定符号并不总是有相同的 ID 值。在 ID 值相关联的Ruby的处理符号将取决于其创建的顺序。

This means that a given symbol won’t always have the same ID value. The ID value associated a Ruby processes symbols will depend on the order that they are created.

您可以通过启动一个新的Ruby进程,创建一个新的符号,并看着看到的 OBJECT_ID ,然后用不同的符号名重复。在的object_id 应在这两种情况下是相同的,因为它会被参照在符号表中的下一个自由点。你必须要小心这样的红宝石定义了很多符号本身,所以如果你使用其中的一个,你会得到不同的结果。

You can see this by starting a new Ruby process, creating a new symbol and looking at its object_id, and then repeating with a different symbol name. The object_id should be the same in both cases, since it will be referring to the next free spot in the symbol table. You need to be careful doing this as Ruby defines a lot of symbols itself, so if you use one of these you’ll get different results.

例如, IRB 会话:

2.1.0 :001 > Symbol.all_symbols.find {|sym| sym.to_s == 'matt' }
 => nil 
2.1.0 :002 > :matt.object_id
 => 542248

和另一个:

2.1.0 :001 > Symbol.all_symbols.find {|sym| sym.to_s == 'banana' }
 => nil 
2.1.0 :002 > :banana.object_id
 => 542248 

下面,我们先检查,看看我们是否要使用已经不存在为标志,名称,然后我们创建符号并查看其 OBJECT_ID 。在这两种情况下,在同一个 542248 ,相应的 ID 2118,即使他们有不同的名称(这些值可能在不同的系统或Ruby版本)有所不同。

Here we first check to see if the name we are going to use doesn’t already exist as a symbol, then we create the symbol and look at its object_id. In both cases it is the same 542248, corresponding to an ID of 2118, even though they have different names (these values may differ on different systems or Ruby versions).

这篇关于Id2sym&安培; symbol.object_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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