void foo(int &x) -> Ruby? Passing integers by refe

2020-03-26 23:47发布

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 ?

2条回答
Explosion°爆炸
2楼-- · 2020-03-27 00:07

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).

查看更多
家丑人穷心不美
3楼-- · 2020-03-27 00:33

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

查看更多
登录 后发表回答