Hi,
In message "[ruby-talk:00885] Re: local / dynamic variables"
on 99/10/30, ts <decoux / moulon.inra.fr> writes:
>G> a = [4,5,6,3]
>G> sum = 0
>G> a.each{|i| sum += i}
>G> p sum
>
> Well it's not really a use of this feature, because sum is not defined
>between | |. Only i is between | | and in this case :
> * sum is a local variable
> * i is a dynamic variable
Oops! I'm sorry for showing wrong example :-(
Well, I try making another:
#
# find the first line matching a pattern and report with its line
#
lines = %w(foo bar baz quux).join("\n") # input data
w, i = nil, lines.size + 1 # sentinels
(lines + "\n").each_with_index{|w,i| break if /z/ =~ w}
unless i > lines.size
puts "z found at line #{i+1} (#{w.chomp!})"
else
puts "no such line"
end
By the way, those variables can be distinguished:
i = 0; [1].each{|i| p defined?(i)} #=> "local-variable"
[1].each{|i| p defined?(i)} #=> "local-variable(in-block)"
-- gotoken