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


Groups > comp.lang.ruby > #2920

Re: scope of assignments with lambda

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail
From Kevin Mahler <kevin.mahler@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: scope of assignments with lambda
Date Fri, 15 Apr 2011 01:37:17 -0500
Organization Service de news de lacave.net
Lines 43
Message-ID <90f821efcacb67d7fe0de170d6979e29@ruby-forum.com> (permalink)
References <5ab8b94d707235d7b83c9b011b696ef4@ruby-forum.com> <2b8e0c638f80e0062cbba1f68ca297d8@ruby-forum.com> <470a9e96982d7a927f140193f12c0f64@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1302850299 54585 65.111.164.187 (15 Apr 2011 06:51:39 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Fri, 15 Apr 2011 06:51:39 +0000 (UTC)
In-Reply-To <470a9e96982d7a927f140193f12c0f64@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 381610
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <90f821efcacb67d7fe0de170d6979e29@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:2920

Show key headers only | View raw


Fearless Fool wrote in post #988777:
> What I was surprised about was the scoping of variables in assignment
> statements.  I guess it makes sense.  Assume that p is previously
> undefined, then a reference on the RHS to p like this:
>
>   p = p + 1
>
> will raise an error.  But wrapped inside a lambda, the reference to p is
> deferred, so it all works:
>
>   p = lambda { |n| p.call(n-1) if (n > 0) }
>
> As I said, that's the power of a lexical closure.

"The scoping of variables in assignment" is exactly the same in both
cases.

p = p + 1 does not fail because p is undefined. p is defined. Its
value is nil. The error results from NilClass#+ not existing. This has
nothing to do with "the power of the lexical closure".

Local variables are discovered at compile time. p gets defined at the
beginning of the scope, *before* the line "p = p + 1", and it's
assigned the value of nil. Try

  p = p.to_i + 1

and observe the non-error. Even better, try

  def foo
    puts local_variables #=>bar
    bar = 99
  end

The reference to p inside the lambda is not deferred. At compile-time,
the p inside the lambda is bound to the local variable p, just as the
p on the right-hand side of "p = p + 1" is bound to the local variable
p. There is nothing special going on as a result of the lambda being
present.

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

Back to comp.lang.ruby | Previous | Next | Find similar | Unroll thread


Thread

Re: scope of assignments with lambda Kevin Mahler <kevin.mahler@yahoo.com> - 2011-04-15 01:37 -0500

csiph-web