[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Iterators and Assigment

Godspeed

2/28/2006 1:20:00 PM

Consider...

def fred
yield $k
puts "in fred #{$k}"
end

$k=2
fred {|o| o = 3}
puts "outside fred #{$k}"

produces:

in fred 2
outside fred 2

why is $k still 2 after the call to fred?

Thanks in advance.


3 Answers

Robert Klemme

2/28/2006 1:37:00 PM

0

Godspeed wrote:
> Consider...
>
> def fred
> yield $k
> puts "in fred #{$k}"
> end
>
> $k=2
> fred {|o| o = 3}
> puts "outside fred #{$k}"
>
> produces:
>
> in fred 2
> outside fred 2
>
> why is $k still 2 after the call to fred?

Because Ruby does call by value where values are object references. Same
holds for blocks and their parameters. There is no implicit aliasing
between k$ and o. When the block is invoked you get a new reference which
initially points to the same instance as $k. Then you assign it and it
points to some other object leaving $k still pointing to the old instance.

Kind regards

robert

Jonathan Leighton

3/1/2006 2:42:00 AM

0

On Tue, 2006-02-28 at 22:38 +0900, Robert Klemme wrote:
> Godspeed wrote:
> > Consider...
> >
> > def fred
> > yield $k
> > puts "in fred #{$k}"
> > end
> >
> > $k=2
> > fred {|o| o = 3}
> > puts "outside fred #{$k}"
> >
> > produces:
> >
> > in fred 2
> > outside fred 2
> >
> > why is $k still 2 after the call to fred?
>
> Because Ruby does call by value where values are object references. Same
> holds for blocks and their parameters. There is no implicit aliasing
> between k$ and o. When the block is invoked you get a new reference which
> initially points to the same instance as $k. Then you assign it and it
> points to some other object leaving $k still pointing to the old instance.
>
> Kind regards
>
> robert

So, try this:

def fred
$k = yield
puts "in fred #{$k}"
end

$k = 2
fred { 3 }
puts "outside fred #{$k}"

Returns:

in fred 3
outside fred 3

You can call yield with parameters if you need the value of $k to be
accessible inside the block. Although $k is accessible inside the block
anyway as it's a global. Although you're probably not using a global in
your actual code.

Regards

--
Jonathan Leighton
http://turnips... | http://jonathanlei... | http://digital-...



Godspeed

3/1/2006 12:22:00 PM

0

Thanks guys. Your responses have cleared this up. I was assumig some sort of
substitution (of the block into the def) going on, but I realise it is the
other way round, the parameters to yield are parameters to the block. Makes
perfect sense now.

"Jonathan Leighton" <lists@turnipspatch.com> wrote in message
news:1141149129.8908.8.camel@localhost.localdomain...
> On Tue, 2006-02-28 at 22:38 +0900, Robert Klemme wrote:
>> Godspeed wrote:
>> > Consider...
>> >
>> > def fred
>> > yield $k
>> > puts "in fred #{$k}"
>> > end
>> >
>> > $k=2
>> > fred {|o| o = 3}
>> > puts "outside fred #{$k}"
>> >
>> > produces:
>> >
>> > in fred 2
>> > outside fred 2
>> >
>> > why is $k still 2 after the call to fred?
>>
>> Because Ruby does call by value where values are object references. Same
>> holds for blocks and their parameters. There is no implicit aliasing
>> between k$ and o. When the block is invoked you get a new reference
>> which
>> initially points to the same instance as $k. Then you assign it and it
>> points to some other object leaving $k still pointing to the old
>> instance.
>>
>> Kind regards
>>
>> robert
>
> So, try this:
>
> def fred
> $k = yield
> puts "in fred #{$k}"
> end
>
> $k = 2
> fred { 3 }
> puts "outside fred #{$k}"
>
> Returns:
>
> in fred 3
> outside fred 3
>
> You can call yield with parameters if you need the value of $k to be
> accessible inside the block. Although $k is accessible inside the block
> anyway as it's a global. Although you're probably not using a global in
> your actual code.
>
> Regards
>
> --
> Jonathan Leighton
> http://turnips... | http://jonathanlei... |
> http://digital-...
>
>
>