[#863] RDtool-0.5.0 — Toshiro Kuwabara <toshirok@...3.so-net.ne.jp>

Hi,

18 messages 1999/10/23
[#864] Re: RDtool-0.5.0 — matz@... (Yukihiro Matsumoto) 1999/10/26

Hi,

[ruby-talk:00886] Re: local / dynamic variables

From: gotoken@... (GOTO Kentaro)
Date: 1999-10-30 10:47:26 UTC
List: ruby-talk #886
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

In This Thread