From: Eric Mahurin Date: 2008-06-07T00:01:00+09:00 Subject: Re: A crosspost from the Perl Community On Fri, Jun 6, 2008 at 7:29 AM, Robert Klemme wrote: > 2008/6/6 Eric Mahurin : >> In ruby, I sometimes miss the ability to easily reference and >> dereference lvalues. There is always another way, but having lvalue >> references/pointers would sometimes be more elegant. > > Can you give an example? I'm curios because I never missed this and > I'd like to know what I am overlooking. It may be that my mindset is > so Java / Ruby already that I simply do not think in ways that would > make references useful - or I deal with a different class of problems > which can be more easily solved without. A general category would be data structures of a linked set of nodes - trees (i.e. AVL, red-black, splay, B), linked lists (single, double), graph, etc. Each node can be easily represented in Ruby with an object that has one or more instance variables for the "links" where each link holds another node or nil. Many operations may equally apply to the head/root of the data structure or somewhere in the middle (a sub head/root). Not a problem for Ruby as long as this operation doesn't want to modify the link at that point. In other languages we might pass a reference/pointer of the variable with the link so that it could be modified. In Ruby, you'll have come up with another solution: a) pass the node containing the link you want to operate along with something saying which link in the node (you'll probably also need a dummy node at the root/head), b) have a separate object for the link so you can pass it around (i.e. simply an Array of one element), c) pass a lambda that can modify the link. None of these are as elegant as an lvalue reference IMHO. In my early ruby days a few years ago I put some code in the rubyforge "reference" project. I still think having something like this readily available would be useful. External iterators are also like lvalue references and is something ruby is missing (IMHO). An external iterator could be thought of as a superset of the reference/pointer concept (external iterator looks like a pointer in C++). Both (lvalue) references/pointers and external iterators could be implemented with normal ruby classes and methods (in ruby or more efficiently in C). Eric