是否可以在 Ruby 中使用指针? [英] Is it possible to use pointers in Ruby?

查看:58
本文介绍了是否可以在 Ruby 中使用指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为 Ruby 被解释为 C.如果是这样,我如何使用指针的概念和作为 C 环境一部分的其他特性?我们如何利用 C 的强大功能和 Ruby 的简单性?

I think Ruby is interpreted to C. If so, how can I use the concept of pointers and other features that are parts of C environment? How can we utilize the power of C with the simplicity of Ruby?

推荐答案

在 Ruby 中,(几乎)每个变量实际上都是一个对象的引用/指针,例如

In Ruby, (almost) every variable is in fact a reference/pointer to an object, e.g.

a = [0, 1, 23]
b = a
a << 42
p b

将给出 [0, 1, 23, 42] 因为 ab 指向同一个对象.

will give [0, 1, 23, 42] because a and b are pointing to the same object.

所以实际上,您一直在使用指针.

So in fact, you are using pointers all the time.

如果你想像在 C 中一样进行指针运算,这在 Ruby 中是不可能的.

If you want to do pointer arithmetic as in C, this is not possible with Ruby.

这篇关于是否可以在 Ruby 中使用指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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