[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: object reference handle (like perl's reference to scalar

Eric Mahurin

5/5/2005 5:27:00 PM

> >>> Below is what I think is a general purpose solution to
> >> making
> >>> references. I provide several ways to do it. Here is an
> >>> example:
> >>>
> >>> a = (0..5).to_a -> [0,1,2,3,4,5]
> >>> w = ref{"a[2..4]"} # uses eval
> >>> x = a.ref[2..4] # uses [] and []= methods
> >>> y = a.ref("[]","[]=",2..4) # specify methods manually
> >>> z = a.ref(["[]",2..4],["[]=",2..4]) # unique args to
> >> get/set
> >>> [...]
> >>
> >> This looks very interesting, if only from the toying
> >> standpoint. Any
> >> chance of you pushing this a bit with a RubyForge project
> and
> >> a gem release?
> >
> > I just registered this under the name "reference".
>
> That might be a somewhat confusing or misleading term, since
> Ruby
> variables already contain references to objects. Something
> with
> 'wrapper' in it, or something, maybe?

The original intent was to have a way to modify these
references that variables contain. This provides a general way
of doing this. A side effect is that you can do a lot more
though - all you need is a way to get and set the thing you
want to access.

x = ref{:a} # allows you to get/set the reference that :a has

x[] # get the object that :a has
x[]=... # set the object reference that :a has

Here is another example (pass by reference):

def swap(a,b)
tmp = a[]
a[] = b[]
b[] = tmp
end

swap(ref{:x},ref{:y}) # swap the object references in x and y

See how useful this thing is?




Yahoo! Mail
Stay connected, organized, and protected. Take the tour:
http://tour.mail.yahoo.com/mai...



2 Answers

dblack

5/5/2005 6:00:00 PM

0

Austin Ziegler

5/6/2005 5:23:00 PM

0

On 5/5/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
> def swap(a,b)
> tmp = a[]
> a[] = b[]
> b[] = tmp
> end
>
> swap(ref{:x},ref{:y}) # swap the object references in x and y
>
> See how useful this thing is?

x, y = y, x

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca