From: Jean-Hugues ROBERT Date: 2004-05-01T21:07:57+09:00 Subject: Re: def [](v) xx; return yy; end # returned value is ignored !? At 19:59 01/05/2004 +0900, you wrote: >On Sat, 01 May 2004 15:28:05 +0900, Jean-Hugues ROBERT wrote: > > >>> - Reference, a reference is an indirect mean of access to a lvalue's > >>> value that is dereferenced when the value is needed and that also makes > >>> it possible to assign a value to the lvalue at any time. > >There is a problem with treating an lvalue this way, and that is >that not every lvalue is an storage location. In fact only >variables as an lvalue are storage locations, the other ones are >methods (i.e.: a[]=5 means a.[]=(5), a.foo=bar calls the method >foo=(bar) on a). Ruby is cool, a lvalue can be a *method call*. So when I say "lvalue" I mean Ruby's ones, not the subset that some other languages provide. I believe my version of Pointer works with any Ruby valid lvalue: a = [1,2,3] ptr = Pointer.new{ "a[2]"} ptr[] = 4 p a # => [1,2,4] See it at end of page http://www.c2.com/cgi/wiki?SinisterSchemeSampleInRuby >I wouldn't welcome the introduction of references on Ruby, >because it may mess things up. Let's say the method "alter" >takes a reference, and changes the value. Then calling >alter(a) will change the value of a, but this is not obvious >when invoking this method, or look at the code. I agree that using reference is often a bad thing. I came to realize that I needed them for some very special code that I am prototyping. Usually I really don't miss them at all. But when you need it hard, you miss it hard :-) >It may be interesting to be able to access variables indirectly >using binding() (outside eval). >for example: > >a = 2 >binding().local_get(:a) >=> a == 2 You can ! eval( :a, binding()) ? >binding().local_set!(:a, 5) >=> a == 5 eval( "a = 5") or tmp = arbitrary_expr; eval( "a=tmp", binding()) >And also Binding::ivar_set!, Binding::ivar_get and >Binding::get_self eval( "@a", binding) tmp = expr; eval( "@a = tmp", a_binding) that_self = eval( "self", a_ binding) Yours, Jean-Hugues ------------------------------------------------------------------------- Web: http://hdl.handle.net/1030.37/1.1 Phone: +33 (0) 4 92 27 74 17