void foo(int& x) - >红宝石?通过引用传递整数? [英] void foo(int &x) -> Ruby? Passing integers by reference?

查看:178
本文介绍了void foo(int& x) - >红宝石?通过引用传递整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一种调整我的C ++编程作业的方式,我决定不是从书中打开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.

无论如何,我无法将此类函数转换为Ruby

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
}

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

What would be the equivalent ruby code inside a function ?

推荐答案

Ruby是严格按值传递的。总是。

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

以下是几个链接:

  • Java is Pass-by-Value, Dammit! (Scott "JavaDude" Stanchfield)
  • Parameter passing in Java (Jon Skeet)
  • Does Java pass by reference?
  • Java, pass-by-value, reference variables

请注意,虽然所有这些都说Java,他们应该真正说Smalltalk及其后代其中包括Java,Ruby和其他语言。

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按值传递引用。但该句中的参考一词与参考中的参考一词不同。也许在我们消除歧义时更清楚:让我们用指针替换通过引用传递和引用(注意,这些是好的良好的poointers,而不是坏的from C):

    • Fortran是通过变量

    • Ruby是pass-by-value,


  • Persistent Data Structures and Managed References - Clojure's Approach to Identity and State - Rich Hickey - QCon London 2009
  • Are We There Yet? - A deconstruction of object-oriented time - Rich Hickey - JVM Language Summit

BTW:我故意用面向对象的OO拼写poointers,以清楚地说明我不是在谈论原始内存地址,我说的是对对象的不透明引用(并且出于显而易见的原因,我不想要使用单词reference;如果你知道一个更好的单词既不是指针也不是参考,我很想听到它)。

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

这篇关于void foo(int& x) - >红宝石?通过引用传递整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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