[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/7/2005 12:11:00 PM


--- Dave Burt <dave@burt.id.au> wrote:
> "Eric Mahurin" <eric_mahurin@yahoo.com> queried:
>
> >> (ref.rb)
>
> >I tried this out and it doesn't seem to do anything. I did
> a
> > require "delegate" followed by the code below. Could you
> give
> > a concrete example - working on mutable (string or array)
> and
> > immediate obects (i.e. Fixnum). Even if you get this to
> work
> > on the mutable objects, I don't see it working on the
> immediate
> > objects.
>
>
> # require 'ref'
>
> irb(main):030:0> a = 1.ref
> => 1
> irb(main):031:0> a.ref = 2 # []= is a synonym for ref=
> => 2
> irb(main):032:0> a # here you can see the ref delegates
> inspect to the
> number 2
> => 2
> irb(main):033:0> a + 1 # and + is delegated too
> => 3
> irb(main):034:0> a.class # class, kind_of?, is_a? and
> respond_to? aren't
> => Ref
> irb(main):035:0> a.deref.class # [] is a synonym for deref
> => Fixnum

Where do I get your ref.db? I saw a link in another post of
yours and it didn't work.

What does this do?

# silly example as x,y = y,x would be better
def swap(a,b)
tmp = a[]
a[] = b[]
b[] = tmp
end
x = 1
y = 2
swap(x.ref,y.ref)
p x
p y
x = "hi"
y = "world"
z = x
swap(x.ref,y.ref)
p x
p y
p z

If this doesn't work, I don't see the real use of the way you
did it. If on the other hand you use evil.rb/become or replace
to assign the dereferenced object, you could at least get the
second case to work (and other non-immediates). If it does
work, great. I want to know how the immediate case is handled.

For now, I'm using replace or become (if available to the
object) to accomplish the above dereferencing (to an object)
assignment. And I still have all the other types of references
(to a variable, assignable expression, attribute/member,
get/set methods, etc) in place. I need to document what I
have, package it into a gem and upload to rubyforge.

> Maybe ref= should be called deref=. But there it is.

yes, this is a dereferencing modify. You are not modifying the
reference itself.

I really don't like the idea of your references looking like
the underlying object. This just confuses things. A reference
is a reference and you can get/set the object that it
references. If you want to get info about the object that it
references, just get it and query it by getting it from the
reference. Why muddy what the reference is?




__________________________________
Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.
http://mobile.yahoo.com/...


3 Answers

Austin Ziegler

5/7/2005 12:37:00 PM

0

On 5/7/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
> > Maybe ref= should be called deref=. But there it is.
> I really don't like the idea of your references looking like the
> underlying object. This just confuses things. A reference is a
> reference and you can get/set the object that it references. If
> you want to get info about the object that it references, just get
> it and query it by getting it from the reference. Why muddy what
> the reference is?

Because no one else is going to write their libraries to check for
reference holders.

Which means that the use of a Ref is, at best, extremely limited.

I won't personally use a library that requires that I use a Ref, as
I find it a concept that is alien to Ruby -- and introduces a really
ugly syntax, to boot. (The C/C++ &foo, *foo, foo->bar looks a lot
cleaner by comparison than foo[].bar)

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



Dave Burt

5/7/2005 12:40:00 PM

0

"Eric Mahurin" <eric_mahurin@yahoo.com>:
> Where do I get your ref.db? I saw a link in another post of
> yours and it didn't work.

Sorry, my fault for posting a link that didn't exist yet.
http://www.dave.burt.id.au/r... is there now.
It may disappear when I become sufficiently ashamed of it.

>
> What does this do?
>
> # 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"

> If this doesn't work, I don't see the real use of the way you
> did it. If on the other hand you use evil.rb/become or replace
> to assign the dereferenced object, you could at least get the
> second case to work (and other non-immediates). If it does
> work, great. I want to know how the immediate case is handled.

I'm not being evil. My Ref is just an extra wrapper for an object.

> For now, I'm using replace or become (if available to the
> object) to accomplish the above dereferencing (to an object)
> assignment. And I still have all the other types of references
> (to a variable, assignable expression, attribute/member,
> get/set methods, etc) in place. I need to document what I
> have, package it into a gem and upload to rubyforge.
>
> I really don't like the idea of your references looking like
> the underlying object. This just confuses things. A reference
> is a reference and you can get/set the object that it
> references. If you want to get info about the object that it
> references, just get it and query it by getting it from the
> reference. Why muddy what the reference is?

You're right, there's no real advantage to be gained here. You might as well
use
Ref = Struct.new(:deref)
That provides a wrapper for an object - just treat it as if it's name ends
in ".deref":
def swap(a, b)
a.deref, b.deref = b.deref, a.deref
end

Cheers,
Dave


Raymond C. Fischer, 90 Hunt Way, Campbell, CA 95008; 408-374-1969

11/12/2011 2:30:00 AM

0

Summary: Ray is a classic usenet zoo monkey. Liars, once they have
been exposed, frequently revert to behavior most similar to a zoo
monkey who sits in the cage throwing feces at passersby but saying
nothing of value. Note that when you reply to a Proven Liar you
encourage them to continue lying.

Raymond C. Fischer (54)
(Louise Fischer)
90 Hunt Way
Campbell, CA 95008
408-374-1969

[][][][][][]
The DemocRAT Hall Of Shame http://www.democrathallof... asks
"Why do you always LIE?"

[Courtesy of Buster Norris]

On 14 Aug 2011 17:56:15 GMT, rfischer@sonic.net (Ray Fischer) wrote:
>>http://www.whitehousedossier.com/2011/08/09/michelle-obama-takes-secret-vacati...

You said she travels with Obogus...........

You LIED!!!!!!!!!!!!!!

She traveled WITHOUT Obogus.........

You ATTEMPTED to claim she DID NOT travel on taxpayer's money........

"Mrs. Obama and family members flew on a Boeing C-40B, the military
version of a 737 that serves as Air Force 2."

YOU LIED!!!!!!!!!!!!!!!!!!!!

HAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!

Posted from:
The DemocRATs Hall of Shame!
http://www.democrathallof...