English 中文(简体)
用地址操纵阵列
原标题:manipulating with arrays by address
  • 时间:2012-01-14 21:30:15
  •  标签:
  • ruby
  • class

帮助我了解法典的这一部分:

class Ooo
 attr_accessor :class_array
end

def func(ctx)
 local_array = ctx.class_array
 local_array = [4,3,5,5,6]
 return
end

aaa = Ooo.new
func(aaa)
aaa.class_array => not [4,3,5,5,6] :-(

I supposed that ruby use addresses when operates with arrays... Why does this code not work? I want to do this (in C):

struct ctx
{
uint class_array[10000]
}

void func(struct *ctx)
{
 uint* local_array = &ctx->class_array
 local_array[0] = 4;
 ctx->class_array[0] => 4
}
最佳回答

问题在于法典的这一部分:

local_array = ctx.class_array
local_array = [4,3,5,5,6]

这是你认为的。 第2行列出了一个新的清单,将其分配给当地变量,从而取代对<代码>ctx. . . . . . .l_array的提及。 <not~code>ctx.class_array。 相当一部分C法典将发挥同样的作用,因此,我认为你在这里没有问题。

在C中,你可以使用点人解决这一问题。 在鲁比,你可能想要:

local_array = ctx.class_array
local_array.replace [4,3,5,5,6]

或简单(更好!)

ctx.class_array = [4,3,5,5,6]

由此,直接翻译您的C方案也将发挥作用:

def func(ctx)
  local_array = ctx.class_array
  local_array[4] = 4
end
问题回答

在您的情形下,<代码>ctx> 例例数中注明阵列物体。 在<代码>func方法中,对同一物体的新提及被指定为 local_array localerra。

当你将<代码>[4,3,5,6]至 local_array时,你没有在<代码>ctx上注明的物体上填写,你将重写在<代码>当地_array上的参考资料,并把它作为新阵列。

就点人而言,你正在做的是:

int array[]     = { 1, 2, 3, 4, 5 };
int new_array[] = { 4, 3, 5, 5, 6 };

int * ctx_array   = array;
int * local_array = ctx_array;

local_array = new_array; // ctx_array still points to array[0]

我认为你想要实现的是:

int ** local_array = &ctx_array;
*local_array = new_array; // ctx_array now points to the new array

鲁比不可能这样间接。 你们只能用这种方法修改物体。

然而,翻译你 C snippet to Ruby 产生完全有效的法典:

def func(ctx)
  local_array = ctx.class_array
  local_array[0] = 4
end

ctx.class_array[0]
 => 4

之所以使用,是因为 local_array系指与ctx.class_array相同的物体。

为什么不 t你跳出你的第一个职能线,而只是直接分配:

def func(ctx)
   ctx.class_array = [1, 2, 3, 4]
end

可以通过调整顺序:

def func(ctx)
 local_array = [4,3,5,5,6]
 ctx.class_array = local_array
 return
end




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签