[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbie: Block Local Variable Inheritance

Andrew C.

6/7/2006 2:55:00 PM

Hi, all.

I'm a total newbie, currently working my way through 'Programming Ruby' by
Dave Thomas et al. I've read a passage about the inheritance of local
variables by blocks (p51 of my edition), and I can't quite make sense of
it... or, rather, I thought I had a vague notion of what was being said, but
I can't come up with any example code that demonstrates what I believe is
being described.

The passage says: "If the parameters to a block are existing local
variables, those variables will be used as the block parameters, and their
values may be changed by the block's execution... If [variables inside the
block] first appeared outside the block, the variables will be shared
between the block and the surrounding environment."

Could someone show me a little code that demonstrates a block changing a
pre-existing local variable, as all my experiments to do so either treat
variables/parameters within the block as local or tells me they're
undefined, so I guess I just don't understand.

Thanks in advance for any assistance.

A.


20 Answers

Andrew C.

6/7/2006 2:59:00 PM

0


"Andrew C." <backwards.moc.liamg@senroc.werdna.backwards> wrote in message
news:pNBhg.183$s4.70@newsfe3-win.ntli.net...
> Hi, all.
>
> I'm a total newbie, currently working my way through 'Programming Ruby' by
> Dave Thomas et al. I've read a passage about the inheritance of local
> variables by blocks (p51 of my edition), and I can't quite make sense of
> it... or, rather, I thought I had a vague notion of what was being said,
> but I can't come up with any example code that demonstrates what I believe
> is being described.
>
> The passage says: "If the parameters to a block are existing local
> variables, those variables will be used as the block parameters, and their
> values may be changed by the block's execution... If [variables inside the
> block] first appeared outside the block, the variables will be shared
> between the block and the surrounding environment."
>
> Could someone show me a little code that demonstrates a block changing a
> pre-existing local variable, as all my experiments to do so either treat
> variables/parameters within the block as local or tells me they're
> undefined, so I guess I just don't understand.
>
> Thanks in advance for any assistance.

The moral of the story is keeping reading the book! :-)

Pretend I didn't post this dumb question, and it can be our little secret...
;-)

A.


Robert Klemme

6/7/2006 3:33:00 PM

0

Andrew C. wrote:
> "Andrew C." <backwards.moc.liamg@senroc.werdna.backwards> wrote in message
> news:pNBhg.183$s4.70@newsfe3-win.ntli.net...
>> Hi, all.
>>
>> I'm a total newbie, currently working my way through 'Programming Ruby' by
>> Dave Thomas et al. I've read a passage about the inheritance of local
>> variables by blocks (p51 of my edition), and I can't quite make sense of
>> it... or, rather, I thought I had a vague notion of what was being said,
>> but I can't come up with any example code that demonstrates what I believe
>> is being described.
>>
>> The passage says: "If the parameters to a block are existing local
>> variables, those variables will be used as the block parameters, and their
>> values may be changed by the block's execution... If [variables inside the
>> block] first appeared outside the block, the variables will be shared
>> between the block and the surrounding environment."
>>
>> Could someone show me a little code that demonstrates a block changing a
>> pre-existing local variable, as all my experiments to do so either treat
>> variables/parameters within the block as local or tells me they're
>> undefined, so I guess I just don't understand.
>>
>> Thanks in advance for any assistance.
>
> The moral of the story is keeping reading the book! :-)

Wow, it took you just 3 minutes to digest the book? Impressive...
:-)

> Pretend I didn't post this dumb question, and it can be our little secret...
> ;-)

My lips are sealed.

Have fun

robert

Frank Swarbrick

6/8/2006 2:51:00 AM

0

Andrew C. wrote:
> Hi, all.
>
> I'm a total newbie, currently working my way through 'Programming Ruby' by
> Dave Thomas et al. I've read a passage about the inheritance of local
> variables by blocks (p51 of my edition), and I can't quite make sense of
> it... or, rather, I thought I had a vague notion of what was being said, but
> I can't come up with any example code that demonstrates what I believe is
> being described.
>
> The passage says: "If the parameters to a block are existing local
> variables, those variables will be used as the block parameters, and their
> values may be changed by the block's execution... If [variables inside the
> block] first appeared outside the block, the variables will be shared
> between the block and the surrounding environment."
>
> Could someone show me a little code that demonstrates a block changing a
> pre-existing local variable, as all my experiments to do so either treat
> variables/parameters within the block as local or tells me they're
> undefined, so I guess I just don't understand.
>
> Thanks in advance for any assistance.

I've been puzzling over this as well. All I can think of is the
following example:

#!/usr/bin/env ruby

class Frank
def Frank.test(x)
yield 1, x
end
end

i = 0
Frank.test("Test") do |i, f|
puts i
puts f
end
puts i
puts f

With the results being:
1
Test
1
blocks.rb:15: undefined local variable or method `f' for main:Object
(NameError)

Since i was defined outside of the block prior to being passed as a
parameter to the block it can be changed (to a value of 1 in this case)
by the method to which the block was passed and will also exist once the
block has 'terminated'.

Since f was not defined outside of the block is no longer exists once
the block has terminated, thus the error on line 15.

If it refers to something else, well, ya got me...

Frank

Robert Klemme

6/8/2006 6:45:00 AM

0

Frank Swarbrick wrote:
> Andrew C. wrote:
>> Hi, all.
>>
>> I'm a total newbie, currently working my way through 'Programming
>> Ruby' by Dave Thomas et al. I've read a passage about the inheritance
>> of local variables by blocks (p51 of my edition), and I can't quite
>> make sense of it... or, rather, I thought I had a vague notion of what
>> was being said, but I can't come up with any example code that
>> demonstrates what I believe is being described.
>>
>> The passage says: "If the parameters to a block are existing local
>> variables, those variables will be used as the block parameters, and
>> their values may be changed by the block's execution... If [variables
>> inside the block] first appeared outside the block, the variables will
>> be shared between the block and the surrounding environment."
>>
>> Could someone show me a little code that demonstrates a block changing
>> a pre-existing local variable, as all my experiments to do so either
>> treat variables/parameters within the block as local or tells me
>> they're undefined, so I guess I just don't understand.
>>
>> Thanks in advance for any assistance.
>
> I've been puzzling over this as well. All I can think of is the
> following example:
>
> #!/usr/bin/env ruby
>
> class Frank
> def Frank.test(x)
> yield 1, x
> end
> end
>
> i = 0
> Frank.test("Test") do |i, f|
> puts i
> puts f
> end
> puts i
> puts f
>
> With the results being:
> 1
> Test
> 1
> blocks.rb:15: undefined local variable or method `f' for main:Object
> (NameError)
>
> Since i was defined outside of the block prior to being passed as a
> parameter to the block it can be changed (to a value of 1 in this case)
> by the method to which the block was passed and will also exist once the
> block has 'terminated'.
>
> Since f was not defined outside of the block is no longer exists once
> the block has terminated, thus the error on line 15.
>
> If it refers to something else, well, ya got me...
>
> Frank

That's precisely what the doc is about. Another example:

module Enumerable
def my_last
l = nil
each {|l| }
l
end
end

>> %w{aaa bb cc ddd}.my_last
=> "ddd"

Kind regards

robert

Andrew C.

6/8/2006 6:45:00 AM

0


"Frank Swarbrick" <infocat@sprynet.com> wrote in message
news:bgMhg.10392$921.6608@newsread4.news.pas.earthlink.net...
> Andrew C. wrote:
>> Hi, all.
>>
>> I'm a total newbie, currently working my way through 'Programming Ruby'
>> by Dave Thomas et al. I've read a passage about the inheritance of local
>> variables by blocks (p51 of my edition), and I can't quite make sense of
>> it... or, rather, I thought I had a vague notion of what was being said,
>> but I can't come up with any example code that demonstrates what I
>> believe is being described.
>>
>> The passage says: "If the parameters to a block are existing local
>> variables, those variables will be used as the block parameters, and
>> their values may be changed by the block's execution... If [variables
>> inside the block] first appeared outside the block, the variables will be
>> shared between the block and the surrounding environment."
>>
>> Could someone show me a little code that demonstrates a block changing a
>> pre-existing local variable, as all my experiments to do so either treat
>> variables/parameters within the block as local or tells me they're
>> undefined, so I guess I just don't understand.
>>
>> Thanks in advance for any assistance.
>
> I've been puzzling over this as well. All I can think of is the following
> example:
>
> #!/usr/bin/env ruby
>
> class Frank
> def Frank.test(x)
> yield 1, x
> end
> end
>
> i = 0
> Frank.test("Test") do |i, f|
> puts i
> puts f
> end
> puts i
> puts f
>
> With the results being:
> 1
> Test
> 1
> blocks.rb:15: undefined local variable or method `f' for main:Object
> (NameError)
>
> Since i was defined outside of the block prior to being passed as a
> parameter to the block it can be changed (to a value of 1 in this case) by
> the method to which the block was passed and will also exist once the
> block has 'terminated'.
>
> Since f was not defined outside of the block is no longer exists once the
> block has terminated, thus the error on line 15.
>
> If it refers to something else, well, ya got me...

You seem to be on the right track.

My original (mis-)understanding was that variables local to the called
method could be made accessible in the block by using the same
variable/parameter names. I'm very glad this initial interpretation of what
I read didn't turn out to be the case because that would have been a
dreadful mix-up of variable scope.

A.


Robert Klemme

6/8/2006 8:45:00 AM

0

Andrew C. wrote:
> My original (mis-)understanding was that variables local to the called
> method could be made accessible in the block by using the same
> variable/parameter names. I'm very glad this initial interpretation of what
> I read didn't turn out to be the case because that would have been a
> dreadful mix-up of variable scope.

Um, but it's exactly like this. Local variables in the method *are*
accessibly in the block much the same as scope nesting works in Java, C
etc. You access a local variable used outside of the block by just
"using the same name". These are all accesses to the same variable
"some_local":

def foo
some_local
whatever.each do |some_local|
...
this_is_block_local
end

some_thing_else.each do
some_local
this_is_block_local
end
end

Regards

robert

Andrew C.

6/9/2006 6:23:00 AM

0


"Robert Klemme" <bob.news@gmx.net> wrote in message
news:4eq6d1F1eb0ujU1@individual.net...
> Andrew C. wrote:
>> My original (mis-)understanding was that variables local to the called
>> method could be made accessible in the block by using the same
>> variable/parameter names. I'm very glad this initial interpretation of
>> what I read didn't turn out to be the case because that would have been a
>> dreadful mix-up of variable scope.
>
> Um, but it's exactly like this. Local variables in the method *are*
> accessibly in the block much the same as scope nesting works in Java, C
> etc. You access a local variable used outside of the block by just "using
> the same name". These are all accesses to the same variable "some_local":
>
> def foo
> some_local
> whatever.each do |some_local|
> ...
> this_is_block_local
> end
>
> some_thing_else.each do
> some_local
> this_is_block_local
> end
> end
>
> Regards
>
> robert

Thanks, but I think we're saying the same thing.

A.


Robert Klemme

6/9/2006 6:48:00 AM

0

Andrew C. <backwards.moc.liamg@senroc.werdna.backwards> wrote:
> "Robert Klemme" <bob.news@gmx.net> wrote in message
> news:4eq6d1F1eb0ujU1@individual.net...
>> Andrew C. wrote:
>>> My original (mis-)understanding was that variables local to the
>>> called method could be made accessible in the block by using the
>>> same variable/parameter names. I'm very glad this initial
>>> interpretation of what I read didn't turn out to be the case
>>> because that would have been a dreadful mix-up of variable scope.
>>
>> Um, but it's exactly like this. Local variables in the method *are*
>> accessibly in the block much the same as scope nesting works in
>> Java, C etc. You access a local variable used outside of the block
>> by just "using the same name". These are all accesses to the same
>> variable "some_local":
>>
>> def foo
>> some_local
>> whatever.each do |some_local|
>> ...
>> this_is_block_local
>> end
>>
>> some_thing_else.each do
>> some_local
>> this_is_block_local
>> end
>> end
>>
>> Regards
>>
>> robert
>
> Thanks, but I think we're saying the same thing.

Ah, ok. Then I don't understand your misunderstanding. :-)

Nevermind

robert

Tracey12

5/26/2012 6:28:00 PM

0

On May 26, 1:22 pm, robw <noddy...@comcast.net> wrote:

> Wow,sploken like a true fraud who's willing to take rushbo, beck, etc.
> up the arse.
>
> You've sevred your idols well!!!

Youd rather see the WTC brought down than the UN building, wouldn't
you?
Your extremism is going to get you in trouble.

Alan Ferris

5/26/2012 6:42:00 PM

0

On Sat, 26 May 2012 11:05:06 -0700 (PDT), Tracey12
<tracey12email@gmail.com> wrote:

>
>The UN is a prime example of renegade authoritarianism ready to
>pounce. The UN is filled with 3rd world thugs and lower forms of
>life. Many of the members of the so called security council hate
>every other nation on earth. Most of the thugs you wouldn't want in
>your neighborhood.

You do realise that it is the US, Russia and China that most often use
the veto and not these 3rd world countries, as you call them.

Like the time the US vetoed the condemnation of Sadam for gassing the
Iranians and the Kurds. Like the way the US vetoes almost every
security council statement against Israel.


--
Ferrit

()'.'.'()
( (T) )
( ) . ( )
(")_(")