Martin DeMello <martindemello / yahoo.com> wrote:
> irb(main):006:0> b.inject(lambda {}) {|a,v| a.call; puts v; a = lambda { puts "-
> --" }}

Actually, I prefer yours over Robert's because his leaves the last
element dangling (could be useful of course).  However, I don't think
the lambda is at all necessary in your case, although I'm sure it
could come in handy in an other situation.

You could simply do:

b.inject("") {|a,v| print a, v; " -- "}

Or if you just want to compute the string instead of just printing:

b.inject {|x,y| "#{x} -- #{y}" }

Although that last doesn't work for what I want to do since I might
have to repeat complex code for both the first element of the
iteration and for y.  There's a fix for this:

b.inject(nil) {|x,y| x ? x += " -- " : x = ""; x += "#{y}" }

So now the complex code only has to be written for y.

I love this.  It's totally crazy!

Cheers,
Navin.