Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4973 > unrolled thread

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

Started byKaye Ng <sbstn26@yahoo.com>
First post2011-05-24 04:45 -0500
Last post2011-05-26 23:59 -0500
Articles 13 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  "Local variable within code blocks do not interfere with those outside the block" Kaye Ng <sbstn26@yahoo.com> - 2011-05-24 04:45 -0500
    Re: "Local variable within code blocks do not interfere with those outside the block" Adam Prescott <adam@aprescott.com> - 2011-05-24 05:08 -0500
      Re: "Local variable within code blocks do not interfere with those outside the block" Adam Prescott <adam@aprescott.com> - 2011-05-24 05:16 -0500
    to Adam Prescott, Kaye Ng <sbstn26@yahoo.com> - 2011-05-26 02:42 -0500
      Re: to Adam Prescott, Adam Prescott <adam@aprescott.com> - 2011-05-26 04:17 -0500
    Re: "Local variable within code blocks do not interfere with those outside the block" Bala TS <bdeveloper01@yahoo.com> - 2011-05-26 04:20 -0500
      Re: "Local variable within code blocks do not interfere with those outside the block" Josh Cheek <josh.cheek@gmail.com> - 2011-05-26 04:57 -0500
      Re: "Local variable within code blocks do not interfere with those outside the block" Adam Prescott <adam@aprescott.com> - 2011-05-26 05:05 -0500
    Re: "Local variable within code blocks do not interfere with those outside the block" Bala TS <bdeveloper01@yahoo.com> - 2011-05-26 07:46 -0500
    Re: "Local variable within code blocks do not interfere with those outside the block" Bala TS <bdeveloper01@yahoo.com> - 2011-05-26 23:09 -0500
    Re: "Local variable within code blocks do not interfere with those outside the block" Bala TS <bdeveloper01@yahoo.com> - 2011-05-26 23:11 -0500
      Re: "Local variable within code blocks do not interfere with those outside the block" Josh Cheek <josh.cheek@gmail.com> - 2011-05-26 23:30 -0500
        Re: "Local variable within code blocks do not interfere with those outside the block" Bala TS <bdeveloper01@yahoo.com> - 2011-05-26 23:59 -0500

#4973 — "Local variable within code blocks do not interfere with those outside the block"

FromKaye Ng <sbstn26@yahoo.com>
Date2011-05-24 04:45 -0500
Subject"Local variable within code blocks do not interfere with those outside the block"
Message-ID<973cac1dd063c9b58788432c1267c14e@ruby-forum.com>
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

-----------------------------------------------------

Or do I not understand the concept? I'm using 1.9.2

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#4975

FromAdam Prescott <adam@aprescott.com>
Date2011-05-24 05:08 -0500
Message-ID<BANLkTinb6W9JSv2FH6O53cBN5dRbRVsBvw@mail.gmail.com>
In reply to#4973
[Note:  parts of this message were removed to make it a legal post.]

On Tue, May 24, 2011 at 10:45 AM, Kaye Ng <sbstn26@yahoo.com> wrote:

> " 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

[toc] | [prev] | [next] | [standalone]


#4976

FromAdam Prescott <adam@aprescott.com>
Date2011-05-24 05:16 -0500
Message-ID<BANLkTinBeT7oZRu_0PLk+Z3RtnHRNn1RSg@mail.gmail.com>
In reply to#4975
[Note:  parts of this message were removed to make it a legal post.]

On Tue, May 24, 2011 at 11:07 AM, Adam Prescott <adam@aprescott.com> wrote:

> 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

[toc] | [prev] | [next] | [standalone]


#5076 — to Adam Prescott,

FromKaye Ng <sbstn26@yahoo.com>
Date2011-05-26 02:42 -0500
Subjectto Adam Prescott,
Message-ID<67a3f63e620fc8ba989ffba0fce9ba88@ruby-forum.com>
In reply to#4973
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. =)

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5078 — Re: to Adam Prescott,

FromAdam Prescott <adam@aprescott.com>
Date2011-05-26 04:17 -0500
SubjectRe: to Adam Prescott,
Message-ID<BANLkTik1Tmy1FOODFvrrY-Ru-ZshNbdRVg@mail.gmail.com>
In reply to#5076
[Note:  parts of this message were removed to make it a legal post.]

On Thu, May 26, 2011 at 8:42 AM, Kaye Ng <sbstn26@yahoo.com> wrote:

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

Mateusz has already explained it, but if you need more, you can visit the
URL I gave just before the code in my post:

http://ruby.runpaint.org/closures#block-local-variables

[toc] | [prev] | [next] | [standalone]


#5079

FromBala TS <bdeveloper01@yahoo.com>
Date2011-05-26 04:20 -0500
Message-ID<4e703d2a03a59f713083806b3fb75289@ruby-forum.com>
In reply to#4973
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5080

FromJosh Cheek <josh.cheek@gmail.com>
Date2011-05-26 04:57 -0500
Message-ID<BANLkTinEQT=qEogDCr0YBUB=1eBV4P1BzA@mail.gmail.com>
In reply to#5079
On Thu, May 26, 2011 at 4:20 AM, Bala TS <bdeveloper01@yahoo.com> wrote:
> 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
>

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

[toc] | [prev] | [next] | [standalone]


#5082

FromAdam Prescott <adam@aprescott.com>
Date2011-05-26 05:05 -0500
Message-ID<BANLkTinNuh1SP-eg=ESVpGnXt4X4JH8YRA@mail.gmail.com>
In reply to#5079
[Note:  parts of this message were removed to make it a legal post.]

On Thu, May 26, 2011 at 10:20 AM, Bala TS <bdeveloper01@yahoo.com> wrote:

> 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.

[toc] | [prev] | [next] | [standalone]


#5087

FromBala TS <bdeveloper01@yahoo.com>
Date2011-05-26 07:46 -0500
Message-ID<d7cc42ee47db27ac654d493441c9d1d1@ruby-forum.com>
In reply to#4973
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5128

FromBala TS <bdeveloper01@yahoo.com>
Date2011-05-26 23:09 -0500
Message-ID<0972b14a1b3d089143d37fec849693d3@ruby-forum.com>
In reply to#4973
I got the result


  please check it.


 by
 Bala Ts

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


-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5129

FromBala TS <bdeveloper01@yahoo.com>
Date2011-05-26 23:11 -0500
Message-ID<abfa84bb4771ac3dbd1e84a4d798507a@ruby-forum.com>
In reply to#4973
I have jpg file Here the results are available(screen shot)

by
bala(bdeveloper01)

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


-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5130

FromJosh Cheek <josh.cheek@gmail.com>
Date2011-05-26 23:30 -0500
Message-ID<BANLkTi=EKZUC_4EeBw+-j4syPrx6hPjrLw@mail.gmail.com>
In reply to#5129
[Note:  parts of this message were removed to make it a legal post.]

On Thu, May 26, 2011 at 11:11 PM, Bala TS <bdeveloper01@yahoo.com> wrote:

> I have jpg file Here the results are available(screen shot)
>
> by
> bala(bdeveloper01)
>
> Attachments:
> http://www.ruby-forum.com/attachment/6232/localvariable.jpg
>
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

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

[toc] | [prev] | [next] | [standalone]


#5134

FromBala TS <bdeveloper01@yahoo.com>
Date2011-05-26 23:59 -0500
Message-ID<ee531e9473b95cc60cf46a94b66a52fe@ruby-forum.com>
In reply to#5130
Josh Cheek wrote in post #1001405:
> On Thu, May 26, 2011 at 11:11 PM, Bala TS <bdeveloper01@yahoo.com>
> wrote:
>
>> Posted via http://www.ruby-forum.com/.
>>
>>
>
> 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)

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web