From: "ts" <decoux / moulon.inra.fr>
> >>>>> "d" == daz  <dooby / d10.karoo.co.uk> writes:
>
> >> def m
> >> [].fetch(2) {return nil}
>
> d>       [].fetch(2) {nil}  # <--------- see Array#fetch w/ block
>
>  No, no : this is volontary. See lib/optparse.rb
>
>     def search(id, key)
>       if list = __send__(id)
>         val = list.fetch(key) {return nil}
>         return val unless block_given?
>         yield(val)
>       end
>     end
>
>
>
> Guy Decoux
>
>

Understood (took me a while).

No assignment is being made to val when
the index lies outside the array.

The 'nil' after 'return' in the optparse.rb clip
isn't strictly necessary.


<Pickaxe>
"A return expression immediately exits a method.
 The value of a return is nil if it is called with
 no parameters, ..."
</>

(sounds obvious but, inside a block, it made me
 think of different scope.)

<ri> Array#fetch
 ... the [block] form returns the value of invoking
 the block, ...
</>

self.note("That use of the verb 'return' is not\
  related to the ruby keyword 'return' !!!")


def roo(n)
  ret = ['A'].fetch(n) {return}
  puts "ZZZ#{n}"
  ret || 42  #  never nil
end

p roo(0)
p roo(1)


#-> ZZZ0
#-> "A"
#-> nil



Thanks,

rosbif ;)

(Can't think of an occasion in 14 months when I
 couldn't understand the anglois.  Continuez svp)