"Local variable within code blocks do not interfere with thoseoutside the block"

K

Kaye Ng

I read this in a book.

" In Ruby 1.9, however, local variables used within code blocks will not
interfere with local variables located outside of the block."

I don't know if my code is wrong, but it looks to me like the local
variable inside the code block DOES interfere with the local variable
(with the same name) outside the code block.

x = [1, 2, 3, 4, 5]
var = 1

x.each do
|number| (var = 10)
end

puts var
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

" In Ruby 1.9, however, local variables used within code blocks will not
interfere with local variables located outside of the block."

I don't know if my code is wrong, but it looks to me like the local
variable inside the code block DOES interfere with the local variable
(with the same name) outside the code block.

x = [1, 2, 3, 4, 5]
var = 1

x.each do
|number| (var = 10)
end

puts var

It's affecting `var` because of the assignment, and because it's not an
argument to the block. You might find
http://ruby.runpaint.org/closures#block-local-variables useful to read
through. Also consider this:

a = 1; [2].each { |x| p a }; a #=> 1; 1
a = 1; [2].each { |x| a = 10; p a }; a #=> 10; 10
a = 1; [2].each { |a| p a }; a #=> 2; 1
a = 1; [2].each { |a| p a; a = 10; p a }; a #=> 2; 10; 1

And with block-local variables in 1.9:

a = 1; [2].each { |;a| p a; }; a #=> nil; 1
a = 1; [2].each { |;a| p a; a = 2; p a }; a #=> nil; 2; 1
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

a = 1; [2].each { |a| p a }; a #=> 2; 1

Important to note that this differs from 1.8:

RUBY_VERSION #=> 1.9.2
a = 1; [2].each { |a| p a }; a #=> 2; 1

RUBY_VERSION #=> 1.8.7
a = 1; [2].each { |a| p a }; a #=> 2; 2
 
K

Kaye Ng

Hi Adam. There's only one thing I don't understand:

a = 1; [2].each { |;a| p a; }; a #=> nil; 1
a = 1; [2].each { |;a| p a; a = 2; p a }; a #=> nil; 2; 1

This is the first time I've seen a variable passed into a code block
with a semicolon preceding it
|;a|
and also semicolon following the variable
p a;

I'm not sure what it does nor do I understand its significance. =)
 
B

Bala TS

Here x is a variable(array_variable) it contains 5 elements

x = [1,2,3,4,5]


x.each do |r|{puts "#{r}"}
end

=> the result like this order
1
2
3
4
5


This code print x.length time print var= 10

x.each do |r|{
var = 10
puts "#{var}"}
end

=> the result like this order
10
10
10
10
10

But you gave
x.each do |r|{
var = 10
}
end
puts "#{var}"

=> then the x.lenth'th last var value is printed here, the the reason u
got ten value

10
 
J

Josh Cheek

Here x is a variable(array_variable) it contains =A05 elements

x =3D [1,2,3,4,5]


x.each do |r|{puts "#{r}"}
end

=3D> the result like this order
1
2
3
4
5

I've never seen code like this in Ruby. In fact, I get a syntax error
when I try to run this code:

untitled:2: syntax error, unexpected tSTRING_BEG, expecting keyword_do
or '{' or '('
x.each do |r|{puts "#{r}"}
^
untitled:2: syntax error, unexpected '}', expecting keyword_end
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

x.each do |r|{
var = 10
puts "#{var}"}
end

=> the result like this order
10
10
10
10
10

But you gave
x.each do |r|{
var = 10
}
end
puts "#{var}"

In addition to the syntax errors, you will find this happens:

NameError: undefined local variable or method `var' for main:Object

`var` is local to the block. Because of that, it doesn't accurately explain
the problem.
 
B

Bala TS

I got the point if you try like this way
Here x is a variable(array_variable) it contains 5 elements

x = [1,2,3,4,5]


x.each do |r|
puts "#{r}"
end

=> the result like this order
1
2
3
4
5


This code print x.length time print var= 10

x.each do |r|
var = 10
puts "#{var}"
end

=> the result like this order
10
10
10
10
10

But you gave
x.each do |r|
var = 10
end
puts "#{var}"

x=[1,2,3,4,5]
x.each do |r|
@var = 10
end
puts "#{@var}"

=> then the x.lenth'th last var value is printed here, the the reason u
got ten value

10
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

I have jpg file Here the results are available(screen shot)

by
bala(bdeveloper01)

Attachments:
http://www.ruby-forum.com/attachment/6232/localvariable.jpg

r.to_s is equal to "#{r}", but more straightforward. So when you want to
convert something to a string, it is better to use its to_s method.

The puts method, though, invokes the to_s method on the object before it
outputs it. So when you are sending an object to puts, you don't need to
worry about whether it is a string at all.

puts "#{r}" # so rather than this
puts r # instead use this
 
B

Bala TS

Josh Cheek wrote in post #1001405:
r.to_s is equal to "#{r}", but more straightforward. So when you want to
convert something to a string, it is better to use its to_s method.

The puts method, though, invokes the to_s method on the object before it
outputs it. So when you are sending an object to puts, you don't need to
worry about whether it is a string at all.

puts "#{r}" # so rather than this
puts r # instead use this


If you want to give some name string then
puts r #is not work
puts "value:#{r}"

=> result should come like this format

value:1
value:2
value:3
value:4
value:5

like this way

by
bala(bdeveloper01)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top