On Thu, 1 Feb 2001, Mathieu Bouchard wrote:

> "return" returns from "real" methods; for closures you have to return
> values "naturally", that is, by the value of the last expression in the
> closure's body.
> 
> This feature allows for things such as:
> 
> class SomeArray
>   def index(v)
>     each_with_index {|x,i|
>        return i if x==v
>     }
>     nil
>   end
> end

Interesting -- I see the workings of it now, though it's still on my
list of POLS question-marks.  Somehow:

  n = Proc.new { return 100 } .call

feels like it should result in n == 100.  

Anyway, tinkering with this reveals some fun subtleties.  For
instance:

  def yes1
    Proc.new { return true } .call
    # not reached
  end

  def yes2
    Proc.new { true } .call
    # reached
  end

  p yes1     # true
  p yes2     # true


Not very real-world, but definitely subtle :-) And, playing with where
the call happens, one can do this:

  def yes3
    Proc.new { true }
  end

  p yes3.call

but not this:

  def yes4
    Proc.new { return true }
  end

  p yes4.call


I'd rather hoped that wrapping the Proc in a named method would make
it feel that it had something to return *from*: 

  def yes5
    Proc.new { return true }
  end

  def wrapper
    yes5.call
  end

but it wasn't fooled:

  p wrapper     # LocalJumpError

(It's that context of creation thing again :-)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav