From: Pedro Silva Date: 2008-09-06T18:53:34+09:00 Subject: Re: Request: arguments by reference Hi Thomas, > If you need badly the variables to really change values, do what should > be done in an object oriented language: wrap them in classes, and add > accessors. That's the way you'd also do it in Java. This is obviously a "solution" but with considerable performance penalty. It's like using C pointers to alter values, so if you need to alter a pointer's value you have a pointer to a pointer and so on... At least in C you don't need to encapsulate everything so you have small performance penalty. For instance, if I call a method that calls another method to change the value I need to encapsulate this two times, that is, I need to encapsulate each time I call a function to allow it to alter the value. > In fact, programming forces the programmer to know how to programme. And > knowing what creates a new object and what not, is part of this > knowledge. And in Ruby it's not very hard to guess. It's very different to know how to programme than to know what a specific method/operator does. Regarding object creation you're right, unless you use strange libraries there's not hard to know what changes and what does not. Suppose a Ruby implementation where += behaves like <<, the final result is the same so I guess this complies with Ruby language specification, but the programmer has to take that into account... > In fact Ruby always passes the reference to an object by value, no > matter if it is Fixnum or whatever. The only difference is that Fixnums > (and some others) are immutable, so they simply do not have methods like > << that change their values in-place. Ruby behaves the same way with any > class of arguments, it's the class itself that does or does not allow an > in-place change. When I said by value I should have written reference by value, my mistake. That's again the problem of encapsulation, the main reason behind, for instance, a Fixnum being immutable is performance. If that didn't meant performance penalty, Ruby wouldn't be implemented this way. The same applies for what I called "company" (symbols, ...). I understand your point, I just wanted to have a simple way to do that as in C++, avoiding an encapsulation for each function invocation. Ruby is an awesome language and im my opinion what's holding it back is it's performance, thinking in a way that has even worse performance isn't a "solution" for me. Pedro. -- Posted via http://www.ruby-forum.com/.