[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Same variable name used in block and local question

rubyrus

2/28/2006 1:50:00 PM

Can anybody help me here, TIA!!

This is the code. I expect to see the string "Afterward:turkey", but
why am I getting "Afterward:camel"? |animal| parameter in a block
should be temporary inside the block, right??


Code:
----------
animal = "turkey"

puts "Original:" + animal
puts

["cow","camel"].each { |animal| puts "in block:" + animal }

puts
puts "Afterwards:" + animal


Output:
-----------
Original:turkey

in block:cow
in block:camel

Afterwards:camel

20 Answers

Robert Klemme

2/28/2006 1:58:00 PM

0

rubyrus wrote:
> Can anybody help me here, TIA!!
>
> This is the code. I expect to see the string "Afterward:turkey", but
> why am I getting "Afterward:camel"? |animal| parameter in a block
> should be temporary inside the block, right??

No, it isn't because it was defined outside the block. It's used in cases
like

count = 0
arr.each { count += 1 }
puts "count=#{count}"

If you want to keep it local you must not define it in the surrounding
scope.

Kind regards

robert

dblack

2/28/2006 2:06:00 PM

0

dblack

2/28/2006 2:14:00 PM

0

Robert Klemme

2/28/2006 3:53:00 PM

0

dblack@wobblini.net wrote:
> Hi --
>
> On Tue, 28 Feb 2006, Robert Klemme wrote:
>
>> rubyrus wrote:
>>> Can anybody help me here, TIA!!
>>>
>>> This is the code. I expect to see the string "Afterward:turkey",
>>> but why am I getting "Afterward:camel"? |animal| parameter in a
>>> block should be temporary inside the block, right??
>>
>> No, it isn't because it was defined outside the block. It's used in
>> cases like
>>
>> count = 0
>> arr.each { count += 1 }
>> puts "count=#{count}"
>>
>> If you want to keep it local you must not define it in the
>> surrounding scope.
>
> I think that's a different thing, though, from the parameter
> semantics. As I understand it, in 2.0 things would work like this:

Is there a difference with regard to scope between

(1..10).each {|foo| }

and

(1..10).each { foo = 1 }

? I think in both cases foo is limited to the block:

16:51:56 [~]: ruby -e 'def f() (1..5).each {|foo|}; foo end; f'
-e:1:in `f': undefined local variable or method `foo' for main:Object
(NameError)
from -e:1
16:51:59 [~]: ruby -e 'def f() (1..5).each {foo=1}; foo end; f'
-e:1:in `f': undefined local variable or method `foo' for main:Object
(NameError)
from -e:1
16:52:04 [~]:

Note, I'm not talking about Ruby 2.

> count = 0
> other = "zero"
> [1,2,3].each {|other| count += 1 }
>
> # count is now 3
> # other is still "zero"
>
> I may be getting it wrong... but I think the scoping of parameter
> variables and other block variables is going to diverge.

That's why I stick to the status quo - I lost track of this discussion
somewhere. But I trust Matz to do it properly. At least all relevant
arguments seem to have been exchanged. :-)

Kind regards

robert

dblack

2/28/2006 4:17:00 PM

0

Dr_Gonzo

7/14/2010 10:09:00 PM

0


> Polar bear wearing white mittens in a blizzard.  LTG :)

Or maybe black bears attacking a Black Forest campground at midnight?

erickit

7/14/2010 10:59:00 PM

0

On Jul 14, 2:53 pm, kbliznick <kblizn...@aol.com> wrote:
> On 14 July, 14:48, Big12bus <w-morris...@ti.com> wrote:
>
> > On Jul 14, 4:26 pm, "Pinball.erie" <pinball.e...@gmail.com> wrote:
>
> > > Weren't they going to announce the new title today?
>
> > They have announced it....
>
> And it is?........

DOHO Gets Her Oats.

Eric

americannleaguer

7/14/2010 11:14:00 PM

0

On Jul 14, 6:58 pm, erickit <erickitr...@msn.com> wrote:
> On Jul 14, 2:53 pm, kbliznick <kblizn...@aol.com> wrote:
>
> > On 14 July, 14:48, Big12bus <w-morris...@ti.com> wrote:
>
> > > On Jul 14, 4:26 pm, "Pinball.erie" <pinball.e...@gmail.com> wrote:
>
> > > > Weren't they going to announce the new title today?
>
> > > They have announced it....
>
> > And it is?........
>
> DOHO Gets Her Oats.
>
> Eric

Whatever title it is; Jack played it; he likes it!

ScottinSGFNY

7/14/2010 11:28:00 PM

0


>
> Whatever title it is; Jack played it; he likes it!

I have heard that straight from the horses mouth before - good post!
Still waiting on my LOTR LE Special gift too........

erickit

7/14/2010 11:31:00 PM

0

On Jul 14, 4:27 pm, ScottinSGFNY <scottcher...@hotmail.com> wrote:
> > Whatever title it is; Jack played it; he likes it!
>
> I have heard that straight from the horses mouth before - good post!
> Still waiting on my LOTR LE Special gift too........

I heard it was a modesty sticker for the HD.

Eric