[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

aliasing variables

Eliah Hecht

2/28/2005 10:02:00 PM

Is there a way to do something like
a = 1
alias(b,a)
a += 1
where the desired effect is that now b == 2?


8 Answers

Hal E. Fulton

2/28/2005 11:58:00 PM

0

Eliah Hecht wrote:
> Is there a way to do something like
> a = 1
> alias(b,a)
> a += 1
> where the desired effect is that now b == 2?
>

It's impossible in the general case, because
a+=1 literally means a=a+1 (in other words,
it creates a new object and assigns that to
a).

Fixnums are not mutable at all. But if an
object is mutable, and an operator modifies
the object rather than creating a new one,
the behavior you want will happen:

a = "cat"
b = a
a << "hode"
puts b # cathode

However, assignment never keeps the same
object:

a = "cat"
b = a
a += "hode"
puts b # cat

In short: Assignment works on variables, not
objects; but methods are called on objects,
not variables.

Make sense?


Hal





Eliah Hecht

3/1/2005 12:05:00 AM

0

I know why it doesn't work, I just want to know how to make it work
(if it's doable without making a wrapper object).


On Tue, 1 Mar 2005 08:57:41 +0900, Hal Fulton <hal9000@hypermetrics.com> wrote:
> Eliah Hecht wrote:
> > Is there a way to do something like
> > a = 1
> > alias(b,a)
> > a += 1
> > where the desired effect is that now b == 2?
> >
>
> It's impossible in the general case, because
> a+=1 literally means a=a+1 (in other words,
> it creates a new object and assigns that to
> a).
>
> Fixnums are not mutable at all. But if an
> object is mutable, and an operator modifies
> the object rather than creating a new one,
> the behavior you want will happen:
>
> a = "cat"
> b = a
> a << "hode"
> puts b # cathode
>
> However, assignment never keeps the same
> object:
>
> a = "cat"
> b = a
> a += "hode"
> puts b # cat
>
> In short: Assignment works on variables, not
> objects; but methods are called on objects,
> not variables.
>
> Make sense?
>
>
> Hal
>
>


Eliah Hecht

3/1/2005 12:05:00 AM

0

I mean, can't I just tell ruby that b refers to a, instead of
referring to a's value?


On Mon, 28 Feb 2005 16:04:20 -0800, Eliah Hecht <eliahhecht@gmail.com> wrote:
> I know why it doesn't work, I just want to know how to make it work
> (if it's doable without making a wrapper object).
>
>
> On Tue, 1 Mar 2005 08:57:41 +0900, Hal Fulton <hal9000@hypermetrics.com> wrote:
> > Eliah Hecht wrote:
> > > Is there a way to do something like
> > > a = 1
> > > alias(b,a)
> > > a += 1
> > > where the desired effect is that now b == 2?
> > >
> >
> > It's impossible in the general case, because
> > a+=1 literally means a=a+1 (in other words,
> > it creates a new object and assigns that to
> > a).
> >
> > Fixnums are not mutable at all. But if an
> > object is mutable, and an operator modifies
> > the object rather than creating a new one,
> > the behavior you want will happen:
> >
> > a = "cat"
> > b = a
> > a << "hode"
> > puts b # cathode
> >
> > However, assignment never keeps the same
> > object:
> >
> > a = "cat"
> > b = a
> > a += "hode"
> > puts b # cat
> >
> > In short: Assignment works on variables, not
> > objects; but methods are called on objects,
> > not variables.
> >
> > Make sense?
> >
> >
> > Hal
> >
> >
>


Hal E. Fulton

3/1/2005 12:07:00 AM

0

Eliah Hecht wrote:
> I know why it doesn't work, I just want to know how to make it work
> (if it's doable without making a wrapper object).

OK, sorry. Someone else will benefit from the explanation. ;)

AFAIK it's impossible without a wrapper object.


Hal



Shalev NessAiver

3/1/2005 1:04:00 AM

0

I did. Thanks a lot.

-Shalev


On Feb 28, 2005, at 7:06 PM, Hal Fulton wrote:
>
> OK, sorry. Someone else will benefit from the explanation. ;)
>



Assaph Mehr

3/1/2005 1:16:00 AM

0


Eliah Hecht wrote:
> I mean, can't I just tell ruby that b refers to a, instead of
> referring to a's value?

To my understanding no. All variables are at the same distance from the
objects they refer to. There is no 'variable' object, variables are
just local names for objects. Thus a variable can only refer to an
object, not another variable. You can not create a pointer-to-pointer
using variables.

You can create wrapper objects using delegate.rb in the std-lib. This
is the closest to what you need.

irb(main):001:0> require 'delegate'
=> false
irb(main):003:0> foo = SimpleDelegator.new 'a'
=> "a"
irb(main):004:0> bar = foo
=> "a"
irb(main):005:0> foo == bar
=> true
irb(main):006:0> foo.__setobj__ 'b'
=> "b"
irb(main):007:0> foo == bar
=> true
irb(main):008:0> bar
=> "b"

Note however that if you simply assign to foo (e.g. foo = 'b') you will
rebind the local name 'foo' to another object (the string 'b'). The
local name 'bar' will still reference the delegator object.

HTH,
Assaph

James Britt

3/1/2005 5:31:00 AM

0

Eliah Hecht wrote:
> I mean, can't I just tell ruby that b refers to a, instead of
> referring to a's value?
>
>
> On Mon, 28 Feb 2005 16:04:20 -0800, Eliah Hecht <eliahhecht@gmail.com> wrote:

# Entire needlessly included lengthy prior post snipped

Can I ask that people try, when replying to a post, a) trim all but the
parts of the prior post that are needed for context, and b) not top-post
(that is, please do not put the reply *before* the text being replied to).


Thanks,


James




Eliah Hecht

3/1/2005 7:10:00 AM

0

On Tue, 1 Mar 2005 14:31:07 +0900, James Britt
<jamesUNDERBARb@neurogami.com> wrote:
> Can I ask that people try, when replying to a post, a) trim all but the
> parts of the prior post that are needed for context, and b) not top-post
> (that is, please do not put the reply *before* the text being replied to).
>
Sorry about that; Gmail's got me spoiled.
My bad,
-Eliah.