" 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.
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
···
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.
" 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
Others are handling your question well enough, but I have a comment.
It is more idiomatically Rubyish to format your block like this:
x.each do |number|
var = 10
end
. . . rather than like this:
x.each do
>number> (var = 10)
end
I would find the latter style (your version) much more difficult to read
for complex code samples, and I suspect I am not alone in that.
···
On Tue, May 24, 2011 at 06:45:10PM +0900, Kaye Ng wrote:
This is the first time I've seen a variable passed into a code block
with a semicolon preceding it
>;a|
This makes the variable's scope local to block (so when you change it in
block it doesn't change outside of it).
All the variables before semicolon can be yielded to block by method and
variables after the semicolon are local to block.
I think it was introduced in 1.9, but I'm not really sure about that.
and also semicolon following the variable
p a;
This one just marks the end of statement.
Hope my explanation helps.
This code is not correct -- and furthermore, the interpoation syntax you
are using is unnecessary. Try this:
x.each {|r| puts r }
The block variable should be identified *inside* the opening of the
block, and when using braces you do not need "do" and "end".
Alternatively, if you want to use "do" and "end", you do not need braces:
x.each do |r|
puts r
end
The only reason to use the interpolation syntax here is if you want to
output more than just the contents of the block variable:
x.each {|r| puts "At #{Time.now}, r contains #{r}."; sleep 3 }
At Thu May 26 11:37:25 -0600 2011, r contains 1.
At Thu May 26 11:37:28 -0600 2011, r contains 2.
At Thu May 26 11:37:31 -0600 2011, r contains 3.
At Thu May 26 11:37:34 -0600 2011, r contains 4.
At Thu May 26 11:37:37 -0600 2011, r contains 5.
=> [1, 2, 3, 4, 5]
···
On Thu, May 26, 2011 at 06:20:14PM +0900, Bala TS wrote:
Here x is a variable(array_variable) it contains 5 elements
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
···
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)
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)
···
On Thu, May 26, 2011 at 11:11 PM, Bala TS <bdeveloper01@yahoo.com> > wrote: