void foo(int &x) ->;红宝石?通过引用传递整数?

发布于 2024-08-29 13:52:11 字数 247 浏览 7 评论 0原文

作为给我的 C++ 编程作业增添趣味的一种方式,我决定不再将书中的 C++ 输入到我的计算机上,而是用 Ruby 对其进行改造。是的,这有点傻,但我很无聊。

不管怎样,我在将这种函数转换为 Ruby 时遇到了麻烦。

void swap(int &a,int &b){
  int c=b;
  b=a;
  a=c
}

函数内的等效 ruby​​ 代码是什么?

as a way to spice up my C++ programming homework, I've decided to instead of typing the C++ from the book onto my computer, instead reforming it in Ruby. Yes it's a bit silly, but I'm bored.

Anyway, I'm having trouble converting this kind of function to Ruby

void swap(int &a,int &b){
  int c=b;
  b=a;
  a=c
}

What would be the equivalent ruby code inside a function ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-09-05 13:52:11

Ruby 是严格按值传递的。总是。但有时这些值是指针。

这里有几个链接:

请注意,虽然所有这些都说“Java”,但它们实际上应该说“Smalltalk 及其后代”,其中包括 Java、Ruby 和大量其他语言。

我认为大部分混乱源于两个问题:

  1. Ruby 按值传递引用。但是该句子中的“引用”一词与“按引用传递”中的“引用”一词不同。当我们消除歧义时,也许会更清楚:让我们将“按引用传递”替换为“按变量传递”,将“引用”替换为“指针”(请注意,这些是“好的”行为良好的指针,而不是“坏的”) “来自 C) 的:
    • Fortran 是传递变量
    • Ruby 是按值传递的,它传递的值大部分是指针
  2. 传递的引用(指针)指向可变对象。因此,虽然您无法更改引用,但您可以改变引用指向的对象。这里的问题是 Ruby(像大多数命令式面向对象语言一样)混淆了身份、状态和值的概念。您可以在此处了解有关该问题以及如何修复它的更多信息(请注意,虽然他们说“Clojure”,但所提出的概念是通用的,并且可以同样很好地应用于 OO):

顺便说一句:我故意将“指针”与面向对象的 OO 拼写错误,以表明我不是在谈论原始内存地址,我是在谈论原始内存地址。谈论对对象的不透明引用(出于明显的原因,我不想使用“引用”这个词;如果您知道一个既不是“指针”也不是“引用”的更好的词,我很乐意听到它)。

Ruby is strictly pass-by-value. Always. But sometimes those values are poointers.

Here's a couple of links:

Note that while all of these say "Java", they should really say "Smalltalk and its descendants", which includes Java, Ruby and a ton of other languages.

I think most of the confusion stems from two problems:

  1. Ruby passes references by value. But the word "reference" in that sentence is not the same as the word "reference" in "pass-by-reference". Maybe it is clearer when we disambiguate: let's replace "pass-by-reference" with "pass-by-variable" and "reference" with "pointer" (note that these are "good" well-behaved poointers, not the "bad" ones from C):
    • Fortran is pass-by-variable
    • Ruby is pass-by-value and the values it passes are mostly poointers
  2. The references (poointers) that Ruby passes point to mutable objects. Therefore, while you cannot change the reference, you can mutate the objects that the reference points to. The problem here is that Ruby (like most imperative object-oriented languages) confuses the concepts of identity, state and value. You can learn more about that problem and how to fix it here (Note that while they say "Clojure", the concepts that are presented are universal and could be applied equally well to OO):

BTW: I deliberately misspelt "poointers" with an OO for object-orientation to make it clear that I am not talking about raw memory addresses, I am talking about opaque references to objects (and for obvious reasons I do not want to use the word "reference"; if you know a better word that is neither "pointer" nor "reference", I'd love to hear it).

累赘 2024-09-05 13:52:11

在 Ruby 中,参数是按值传递的。因此,以下方法永远不会产生任何效果:

def doesnt_swap(a, b)
  c = a
  a = b
  b = c
end

另一方面,大多数对象都是引用,例如字符串,因此您可以编写

def swap_strings(a, b)
  c = a.dup
  a.replace(b)
  b.replace(c)
end

这将交换两个参数的字符串值。

整数是立即数,因此没有等价于replace;你不能写swap_integers

无论如何,在 Ruby 中,您可以通过编写 a, b = b, a 来进行交换

In Ruby, arguments are passed by value. So the following method will never have any effect:

def doesnt_swap(a, b)
  c = a
  a = b
  b = c
end

On the other hand, mosts objects are references, for examples strings, so you could write

def swap_strings(a, b)
  c = a.dup
  a.replace(b)
  b.replace(c)
end

This would swap the string values of the two arguments.

Integers are immediates, so there is no equivalent to replace; you can't write swap_integers.

Anyways, in Ruby, you swap by writing a, b = b, a

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文