shiningthrough

A comparison of Ruby pass by reference and C++ pass by reference

12 April 2010

Take the following Ruby and C++ applications:

def do_something(text)
  text = "bar"
end

text = "foo"
do_something(text)
puts text

The Ruby program above outputs "foo"

#include <iostream>
using namespace std;

void DoSomething (std::string &text)
{
  text = "bar";
}

int main ()
{
  std::string text = "foo";
  DoSomething(text);
  cout << text << "\n";

  return 0
}

The C++ program above outputs "bar"

Comments

vexer

I would say that Ruby references were more similar to C++ pointers than they were to C++ references.

shiningththrough

I would agree with you.