[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

variable pointer

akbarhome

11/1/2006 2:43:00 PM

@c = "donal"
@b = @c

abrakadabra(@b, @c)

@c = "mickey"

print @b resulted "mickey"

Is there something like abrakadabra method in ruby? The only thing I
can think to emulate this is using global variable.

Thank you.

16 Answers

Austin Ziegler

11/1/2006 2:53:00 PM

0

On 11/1/06, akbarhome <akbarhome@gmail.com> wrote:
> @c = "donal"
> @b = @c
>
> abrakadabra(@b, @c)
>
> @c = "mickey"
>
> print @b resulted "mickey"
>
> Is there something like abrakadabra method in ruby? The only thing I
> can think to emulate this is using global variable.

No. Variables aren't pointers or locations; they're labels for
objects. By changing @c you have changed its object. You haven't
changed @c.

However, you can trick it out with a Var (or Ref) object (google
around for various implementations) where you can set the value,
something like:

c = Ref.new("donald")
b = c
c._value = "mickey"
puts b

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Victor 'Zverok' Shepelev

11/1/2006 2:54:00 PM

0

akbarhome (akbarhome@gmail.com)
1/11/2006 16:45:17

> @c = "donal"
> @b = @c
>
> abrakadabra(@b, @c)
>
> @c = "mickey"
^^^^ the problem is here

In ruby, var = value is not "change var's value", but rather "set name var for value".
Then, after
@b = @c
@c and @b "points" to same value, and if you change this value, you would see new value through @b and through @c.

@c = "donal"
@b = @c
@c.upcase!
print @b #=> DONAL

but when you do
@c = "mickey"

@c becames to 'point' to "mickey", and @b - still to "donal".
AFAIK, there's no way to change @b in correspondence.

V.


dblack

11/1/2006 3:01:00 PM

0

Ara.T.Howard

11/1/2006 3:25:00 PM

0

dblack

11/1/2006 3:38:00 PM

0

Ara.T.Howard

11/1/2006 3:56:00 PM

0

Wilson Bilkovich

11/1/2006 4:08:00 PM

0

On 11/1/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Thu, 2 Nov 2006, ara.t.howard@noaa.gov wrote:
>
> > On Thu, 2 Nov 2006 dblack@wobblini.net wrote:
> >
> >> It was recently noted on the Rails mailing list that something like
> >> that happens in ActiveRecord. Given Person and Address models, where
> >> Person has_one Address:
> >>
> >> a = person.address
> >> person.address = Address.find(some_other_address)
> >> puts a # a has changed!
> >>
> >> I find it un-Rubyish and disconcerting (though I'm still waiting for
> >> someone to explain the rationale; there may be something I'm missing).
> >
> > can you show an example? are you saying that activerecord avoids apparent
> > copy-on-write semantics with some trickery?
>
> Here's an example from a program which models Ideas; each Idea belongs
> to a Page.
>
> >> i = Idea.find(6)
> => #<Idea:0xb79eec2c @attributes={"page_id"=>"6", "id"=>"6",
> "content"=>"Go to Paris"}>
>
> # Save the idea's page in p
> >> p = i.page
> => #<Page:0xb79e4a88 @attributes={"scratchpad_id"=>"6",
> "title"=>"Domestic travel", "id"=>"6"}>
>
> # Change the idea's page to a different one
> >> i.page = Page.find(7)
> => #<Page:0xb79d1b18 @attributes={"scratchpad_id"=>"1",
> "title"=>"Foreign travel", "id"=>"7"}>
>
> # Examine the saved page -- it's the new one
> >> p
> => #<Page:0xb79d1b18 @attributes={"scratchpad_id"=>"1",
> "title"=>"Foreign travel", "id"=>"7"}>
>
> I don't know the rationale. When I (think I'm) assign(ing) an object
> to a variable, I really don't want it to turn out that it's some
> special arrangement whereby the variable is subject to reassignment
> without notice. I expect:
>
> p = i.page
>
> to be exactly equivalent to:
>
> p = Idea.find(i.page.id)
>
> with respect to p.
>

I must be missing something. Isn't this how virtually all Ruby code works?

class Foo
attr_accessor :bar
end

f = Foo.new
f.bar = "some_string"

p = f.bar
f.bar << " and how!"
puts p

What makes the Rails version of this unexpected?

James Gray

11/1/2006 4:12:00 PM

0

On Nov 1, 2006, at 10:07 AM, Wilson Bilkovich wrote:

> f.bar << " and how!"

Change that line to:

f.bar = "new string"

James Edward Gray II

dblack

11/1/2006 4:20:00 PM

0

Jeremy Henty

11/2/2006 11:26:00 AM

0

On 2006-11-01, dblack@wobblini.net <dblack@wobblini.net> wrote:

> Here's an example from a program which models Ideas; each Idea belongs
> to a Page.
>
>>> i = Idea.find(6)
>=> #<Idea:0xb79eec2c @attributes={"page_id"=>"6", "id"=>"6",
> "content"=>"Go to Paris"}>
>
> # Save the idea's page in p
>>> p = i.page
>=> #<Page:0xb79e4a88 @attributes={"scratchpad_id"=>"6",
> "title"=>"Domestic travel", "id"=>"6"}>
>
> # Change the idea's page to a different one
>>> i.page = Page.find(7)
>=> #<Page:0xb79d1b18 @attributes={"scratchpad_id"=>"1",
> "title"=>"Foreign travel", "id"=>"7"}>
>
> # Examine the saved page -- it's the new one
>>> p
>=> #<Page:0xb79d1b18 @attributes={"scratchpad_id"=>"1",
> "title"=>"Foreign travel", "id"=>"7"}>

Yikes! How is this even possible? Somehow the method is changing the
variable bindings in its caller. Is this related to Binding.of_caller
(which exploited a bug in 1.8.4 and broke when the bug was fixed in
1.8.5 - see
<URL:http://eigenclass.org/hiki.rb?breakpoint+breaking+in...)?

Jeremy Henty