[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6653] each/for binding confusion and resolution

From: grady@... (Steven Grady)
Date: 2000-11-29 23:10:01 UTC
List: ruby-talk #6653
As I am still learning the language, I am hitting things which are
confusing.  I thought I'd share one with people here -- perhaps it
may spark some conversation as to whether it violates POLS.

In essence, one problem I had came down to the following:

    procs = []
    for v in [1, 2]
	procs << proc { v }
    end
    procs.collect { |p| p.call }	-> [2, 2]

I was hoping for [1, 2].

It took me a little while to realize that the reason this would
happen is that the proc uses the variable "v" rather than the
object to which it is bound, and since the binding stays the same
through the loop, the two procs return exactly the same value.

Since I wanted to have each proc return something different, I
thought the right solution would be to somehow create a proc (or
method, or closure, or lambda, or whatever) that interpolated the
_value of "v"_ rather than "v" itself.

After some exploration (and reading a bit of the book), I discovered that
an alternate solution is:
    procs = []
    [1, 2].each { |v|
	procs << proc { v }
    }   
    procs.collect { |p| p.call }	-> [1, 2]

By using <each> rather than <for>, I could have v be local to the loop, and
therefore (?) get a different binding inside each proc.

So now I understand enough to get my original code to work.  I am left
with a few questions:
    Is my analysis correct?  Does <each> create a new binding every time
    through the loop?

    Is this the right way to fix the problem?  It apparently imposes some
    overhead in that there is a binding stored with each proc, and a binding
    resolution occurs each time the proc is called.  I think I'd be happier
    to have a way of specifying that I want the _value_ of "v", analogous
    to the #{} notation in double-quoted strings.

    Is there a straightforward way to fix the code that uses a <for> loop,
    rather than an <each> iterator?  I don't need it, but it would be
    interesting to know of alternate solutions.

Ruby kicks ass.  Thanks for any help.
--
	Steven

"I wish _I_ was a tiger."
"A common lament."

In This Thread

Prev Next