[ABE.pm] Ruby Q

Ricardo SIGNES rjbs-perl-abe at lists.manxome.org
Tue Apr 18 20:05:34 PDT 2006


* Faber Fedor <faber at linuxnj.com> [2006-04-18T22:44:38]
> > The unPerl-like-ness-itude of it is that "a variable's scope begins at the
> > point where its symbol is assigned to" rather than "a variable's scope
> > begins when it is declared to begin."
> 
> Well, that makes sense if you can't declare the variable, i.e. the
> variable is declared once it is assigned.  What am I missing?

  scratchpad_value = 10 # default starting
  user_input.each {
    |datum|
    analysis = MegaWidget.analyze_datum(datum)
    summary  = Reporter.crunch_numbers(analysis)
    scratchpad_valuw = Reporter.average_in(summary, scratchpad_value)
  }

  puts "the final output is #{scratchpad_value}"

In Perl, you would have put "my" in front of only the first assignment to
scratchpad_value, so the assignment to scratchpad_valuw would have been a
compile-time error.  In Ruby, its assignment declares it, but is pointless.
The final output is always 10 and no exception is raised.

-- 
rjbs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.pm.org/pipermail/abe-pm/attachments/20060418/21638572/attachment.bin 


More information about the ABE-pm mailing list