Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Newbie question re. blocks & variable scope

Reply
Thread Tools

Newbie question re. blocks & variable scope

 
 
Steve Cooper
Guest
Posts: n/a
 
      01-18-2004
Hi,

I'd like to ask for some help with local variable scope. I understand
that the scope of a local variable is the "do...end" block in which it
is created. However, the code below shows that the value assigned to
the variable within a block does not persist after the single
iteration in which the variable is given that value, although the
variable is still defined in a later iteration. That is, in the idx=2
iteration, Ruby does not remember that a=11 in the previous iteration,
but it also does not complain about an undefined local variable "a"
(which it of course does outside the block). I haven't been able to
find this behavior mentioned in the documentation or in this
group--can you point me to something that will clarify for me what is
going on?

Thanks,
Steve

data=[1,2]
data.each do | idx|
if idx == 1
a = 11
print "idx=1: a=",a,"\n"
elsif idx == 2
b = 12
print "idx=2: a=",a," b=",b,"\n"
end
end
print "after:\n"
print "a=",a," b=",b,"\n"

>c:\ruby\bin\ruby test.rb

idx=1: a=11
idx=2: a=nil b=12
after:
test.rb:12: undefined local variable or method `a' for main:Object
(NameError)
>Exit code: 1

 
Reply With Quote
 
 
 
 
Shashank Date
Guest
Posts: n/a
 
      01-18-2004

"Steve Cooper" <(E-Mail Removed)> wrote in message
> I'd like to ask for some help with local variable scope. I understand
> that the scope of a local variable is the "do...end" block in which it
> is created.


Correct (so far !

> However, the code below shows that the value assigned to
> the variable within a block does not persist after the single
> iteration in which the variable is given that value, although the
> variable is still defined in a later iteration.


Ah ha ! You are using the term _iteration_ to mean a
"looping construct" (control structure) and then in the example
using "each" which is an "iterator" (method).

In Ruby, a looping construct is _not the same_ as an iterator.
An iterator may be implemented using "yield" and some form
of a looping construct. This is nicely explained in Pickaxe.

> That is, in the idx=2 iteration, Ruby does not remember
> that a=11 in the previous iteration,
> but it also does not complain about an undefined local variable "a"
> (which it of course does outside the block).


Which is because you are using each (iterator).

> I haven't been able to
> find this behavior mentioned in the documentation or in this
> group--can you point me to something that will clarify for me what is
> going on?


Read the later half of Chapter 7 of PickAxe carefully, esp. the sub-topics
"Loops" onwards.

> data=[1,2]
> data.each do | idx|
> if idx == 1
> a = 11
> print "idx=1: a=",a,"\n"
> elsif idx == 2
> b = 12
> print "idx=2: a=",a," b=",b,"\n"
> end
> end
> print "after:\n"
> print "a=",a," b=",b,"\n"



If you rewrite this using a _looping construct_ such as

#----------------------
data=[1,2]
for idx in data
if (idx == 1)
a = 11
puts "idx=#{idx}: a=#{a}"
elsif (idx == 2)
b = 12
p "idx=#{idx}: a=#{a} b=#{b}"
end
end
#----------------------

then you should get the desired behavior.

Does that make sense ?

-- shanko


 
Reply With Quote
 
 
 
 
Kenta MURATA
Guest
Posts: n/a
 
      01-18-2004
In message <(E-Mail Removed) > at Mon, 19 Jan 2004 04:10:03 +0900,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Steve Cooper) wrote:
> I'd like to ask for some help with local variable scope. I understand
> that the scope of a local variable is the "do...end" block in which it
> is created. However, the code below shows that the value assigned to
> the variable within a block does not persist after the single
> iteration in which the variable is given that value, although the
> variable is still defined in a later iteration. That is, in the idx=2
> iteration, Ruby does not remember that a=11 in the previous iteration,
> but it also does not complain about an undefined local variable "a"
> (which it of course does outside the block). I haven't been able to
> find this behavior mentioned in the documentation or in this
> group--can you point me to something that will clarify for me what is
> going on?


Local variable is defined when it is parsed. And block
internal scope is initialized each block call. That is, in
the idx == 2 iteration, Ruby do:

(1) initialize block internal scope.
(2) parse ``a = 11'' line and define variable `a'.
(3) reference varaible `a', but it is not initialized (== nil).

> data=[1,2]
> data.each do | idx|
> if idx == 1
> a = 11
> print "idx=1: a=",a,"\n"
> elsif idx == 2
> b = 12
> print "idx=2: a=",a," b=",b,"\n"
> end
> end
> print "after:\n"
> print "a=",a," b=",b,"\n"
>
> >c:\ruby\bin\ruby test.rb

> idx=1: a=11
> idx=2: a=nil b=12
> after:
> test.rb:12: undefined local variable or method `a' for main:Object
> (NameError)
> >Exit code: 1


--
1024D/2A3FDBE6 2001-08-26 Kenta MURATA (muraken) <(E-Mail Removed)>
Key fingerprint = 622A 61D3 280F 4991 4833 5724 8E2D C5E1 2A3F DBE6


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Newbie question: XSLT Variable scope Colin Walls XML 3 10-10-2004 07:30 AM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments