On 5/29/07, Jon Harrop <jon / ffconsultancy.com> wrote:
> Michael Fellinger wrote:
> > some people use _ as a temporary meaningless variable, just a
> > throw-away so to say.
> > In this case something like
> >
> > hash = {:a => :b, :c => :d}
> >
> > and you are not interested in the :b and :d
> >
> > hash.each do |key, _|
> >   p key
> > end
> >
> > I'm not necessarily a friend of this technique, but it seems easy on
> > the minds of some people.
>
> Right, this is exactly what I guessed it was doing (it is the same in
> SML/OCaml/F#) but what value was being thrown away in the Ruby program and
> where did it come from?
>
>   (1..n).inject(x) { |acc, _| yield(acc) }
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy
> The F#.NET Journal
> http://www.ffconsultancy.com/products/fsharp_journal/?usenet

inject takes a block with two parameters. classic example is a sum of an array:

array.inject(0) {|sum, item| sum + item }

so, in this case, item is not needed, so it is replaced by a variable
with name of "_"
that by convention means "temporary", "throw away"

it can be anything else:
(1..n).inject(x) { |acc, i_dont_need_this| yield(acc) }