Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: object reference handle (like perl's reference to scalar
Eric Mahurin
5/7/2005 2:22:00 PM
I see you modified my example. This doesn't do what the
traditional reference/pointer does in other languages. With a
traditional reference/pointer you do something like this:
C Ruby with Eric's reference.rb
-------------- -----------------------------
p = &var p = ref{:var}
*(p)=value p[]=value # changes var
*(p)==var p[]==var
What you did just appears to create a wrapper around objects to
allow you to make mutable objects out of immutable ones. I
think this is useful, but not a reference/pointer.
But, I am still taking the idea to create a reference like
this:
x.ref
This will make a reference that uses replace/become to set the
object. It cannot genernally be used - especially for
immediates.
Here are the various ways my code can create a reference now:
ref{:x} # reference to the variable x
x.ref # reference to the object in x
ref{"x[1]"} # reference to the assignable expression x[1]
x.ref[1] # reference to the methods [] and []= with 1 as an arg
x.ref("[]",1) # same
x.ref([:"[]",1],[:"[]=",1]) # same - independet get/get
Reference.new(proc{x[1]},proc{|v|x[1]=v}) # explicit get/set
With the exception of the first 2, the rest of the references
will behave the same way - operating on x[1].
--- Dave Burt <dave@burt.id.au> wrote:
> > # silly example as x,y = y,x would be better
> > ...
>
> C:\>ruby
> require 'ref'
> def swap(a,b)
> tmp = a[]
> a[] = b[]
> b[] = tmp
> end
> x = 1.ref
> y = 2.ref
> swap(x, y)
> p x
> p y
> x = "hi".ref
> y = "world".ref
> z = x
> swap(x, y)
> p x
> p y
> p z
> ^Z
> 2
> 1
> "hi"
> "world"
> "hi"
Discover Yahoo!
Stay in touch with email, IM, photo sharing and more. Check it out!
http://discover.yahoo.com/stayin...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: object reference handle (like perl's reference to scalar
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password