From: Robert Klemme Date: 2005-10-03T23:06:48+09:00 Subject: Re: C++ equivs in Ruby David A. Black wrote: > Hi -- > > On Mon, 3 Oct 2005, Robert Klemme wrote: > >> Pascal GUICHARD wrote: >> >>> - on pointers, i try to understand (howto implement) datastructures >>> in which you need to mix values and references/poiters >> >> You cannot use values in Ruby directly. There are only references. Think >> of Ruby as Java without POD's. In practice there are some >> optimizations for Fixnums, Symbols and some other types but >> conceptually (i.e. from a usage point of view) you never see values >> directly. x = nil >> x = Object.new >> x = "foo" >> x = 123 >> >> Here "x" is always a reference variable - like every other variable >> is a reference variable. It's so simple but sometimes difficult to >> get used to - especially when coming from other programming >> languages. > > I think the "immediate value" status of Fixnums etc. goes beyond just > being an optimization, though, and has some practical consequences Which? > and > even some explanatory power. In particular, in: > > x = 123 > > x holds the immediate value 123, rather than a reference to it, But this does not make a difference from a usage point of view: for doing x = x + 1 (or short x += 1) it doesn't make a difference. Also invocation of methods is the same for every x that I showed above. You wouldn't be able to distinguish this from a real reference apart from, maybe, in some esoteric circumstances or in C extensions because basically immediate values are just an optimization. IMHO when starting to use Ruby it's the easiest and least irritating to just assume that every value (aka instance) is referenced and there are no values (at least not directly accessible). If you do numerical calculations you will later start to wonder how Ruby works under the hood and how you make best (most efficient) use of Fixnums etc. But I find it confusing to start with this matter. > which > is the rationale Matz has always given for not having x++ (i.e., it > would be equivalent to doing 123++ and that would make no sense). Hm... I don't know whether I can follow this argument. *If* there was a ++ operator in Ruby, it would probably be the equivalent of x++ <=> ($_,x=x,x.succ;$_) ++x <=> (x=x.succ) Maybe Matz found this (especially in the first case) a too complex construct to be hidden by this unary operator. Dunno. But at the moment I fail to see how this unary operator relates to the fact of "immediate values". Somehow I feel I did not directly hit the point. Darn... Kind regards robert