From: Dave Burt Date: 2005-05-07T21:59:23+09:00 Subject: Re: object reference handle (like perl's reference to scalar) >> My Ref module at http://www.dave.burt.id.au/ruby/ref.rb now tries to >> behave as much like the referred-to object as possible. You above Module >> case statement will work with Refs now, as will uses of respond_to? is_a? >> kind_of? and class. A Ref will additionally respond_to a few unique >> methods (ie deref, deref=). > > Hm, and what do you do if you have to test for Ref objects? variable.ref? > I didn't follow this thread thoroughly, all I can say is that I personally > haven't missed refs in Ruby until now. There are certainly things that > speak in favour of your concept. > > But there are also problems. Ironically some of them seem to be caused by > the fact that you make a Ref behave like the original instance as much as > possible. That way Ref usage is more implicit than explicit and this can > lead to confusion or subtle errors due to aliasing (i.e. you think you got > a copy of something and modify it and some other piece of code breaks > because it was just a ref). You're right about that. I prefer the new implementation: Ref = Struct.new(:deref) > > It still might be worthwile, I just get the feeling that people try to > stuff things from other programming languages into Ruby because they found > them cute / useful / the only way to solve certain things in that > language. IMHO programming languages are different for a reason and often > it's much better to adhere to the old saying "when in Rome do as the > Romans do"... :-) > I don't think what I've done here is anything like that. The concept of a reference here is just a wrapper or pointer, and it has to have a use. Sure, it causes problems in C and Perl, and Ruby definitely offers more elegant tools fo most of the problems that are solved in those languages with pointers, but I'm coming from a Rubyist's perspective here, and all I'm proposing is a new container, for a single object. And I think Austin's PDF::Writer example is one case where it's useful (that is, passing an object by reference into a method so the method can replace the object). Cheers, Dave