From: Massimiliano Mirra Date: 2001-12-30T02:11:23+09:00 Subject: [ruby-talk:29637] Ruby and variables (was Re: Perl/Python Module Porting) (Yes, this is ``a little'' late reply.) On Sun, Dec 09, 2001 at 11:53:37PM -0500, Mathieu Bouchard wrote: > > > 2. In Ruby, there is _no_ equivalent to SCALAR. In Ruby, variables are not > > > objects; in Perl they are fcv ("first-class value") > > Is that the reason why something like this cannot be done? > > class Integer > > def double! > > self = self * 2 > > end > > end > > yes. For that reason I'd call Perl/C++ "variable-oriented", and I'd call > Ruby/Lisp/Smalltalk "value-oriented". > > If you could change the value of "self" in Ruby, following value-oriented > style, it would just mean that the rest of the method is going to be in > the context of a different object; the object originally pointed by > "self" wouldn't be changed, nor would the variable you used in the > expression that called that method. I see. However, there *appears* to be some inconsistency. There is no immediately evident reason to the end developer (this is different from no reason at all!) why a string or an array, for example, can do some inplace modifications (even from inside the class itself) while a Numeric can't. One could wonder why you can do: first_name.downcase! ...while you can't do: angle.to_rad! (or angle.deg2rad!) But also why you can put some heavily modifying behaviours inside the class's black box: class String def kill_a! self.gsub!(/a/, '') end def add_world! self << 'world' end end ...and not others, conceptually simpler, if not with workarounds (or I just haven't found the right way, but the very fact that you have to look hard for something conceptualy simple might mean something): class String def total_change! self.gsub(/.*/, '') self << 'hello world' end end I want to state this again: it is not that this behaviour *is* inconsistent. It is not, when one learns Ruby's orientation as far variables are concerned, as you wrote. It just *appears this way* and looks somewhat uneasy with the principle of least surprise. I'd very much like to see a special variable such as ``val'', as someone suggested, but just for operating on the value from inside the class, without needing delegation. The very first example I quoted could be written like: class Numeric def deg2rad! val = self * Math::PI / 180 end end Yet, I'm writing this with no afterthought whatsoever on side effects and pitholes, and there may well be lots of them, so please don't shoot at me. > > (A delegate with @value as the Intenger would be a solution, but I > > wonder: is there any redefinition or inheritance way to do the above?) > I don't think there's much benefit going beyond delegation. I may even do > explicit #value/#value= instead. My main use for integer variables would > be for model/view intboxes, scrollbar, sliders, etc; not much more than > that. I assume you refer strictly to Integer and not to Numeric generally. At least, I couldn't picture myself delegating numeric classes each time I have to do some math. Massimiliano