On 25 Mar 2011, at 14:32, Colin Bartlett wrote:

> On Fri, Mar 25, 2011 at 7:52 AM, Robert Klemme
> <shortcutter / googlemail.com> wrote:
>> What about
>> ...
>> res =3D [1]
>> [a,b,c].each {|x| x and res.concat x}
>=20
> I'd also wondered whether Iain Barnett really needed array + array2
> rather than array.concat(array2), and wondered about something
> similar:
>=20
> class Array
>  def concat_not_nil( *args )
>    args.each { |arg| concat(arg) unless arg.nil? }
>    self
>  end
>  def plus_not_nil( *args )
>    new_ary =3D self.dup
>    new_ary.concat_not_nil( *args )#
>  end
> end
>=20

I really appreciate the responses, but if I apply them to the code that =
provoked the question for me, and my dislike of wrapping things in an =
array only to get access to a function or block (which also then removes =
the wrapper), and of creating locals for a single use, then I'm not so =
sure they work. As in, there are so many great shortcuts in Ruby that =
feel very natural too, that some kind of infix operator works well for =
args split over several lines due to their length.

I think this reads very easily, HEADERS is 3 env vars added up into one:

    HEADERS =3D ENV["BLAH_BLAH"]           +=20
              ENV["BLAH_BLAH_BLAH"]      +
              ENV["BLAH_BLAH_BLAH_BLAH"]

to this, which creates an array then destroys it, which kind of masks =
the intention:

    HEADERS =3D [
                ENV["BLAH_BLAH"],
                ENV["BLAH_BLAH_BLAH"],
                ENV["BLAH_BLAH_BLAH_BLAH"]
              ].inject([1]) {|r,x| x and r.concat(x) or r}

If I was going to use a fold or something similar then I would probably =
have done something like this, or used a ternary operator in a fold, =
(knowing the way I think):

    HEADERS =3D [
                ENV["BLAH_BLAH"],
                ENV["BLAH_BLAH_BLAH"],
                ENV["BLAH_BLAH_BLAH_BLAH"]
              ].reject{|x| x.nil? }.reduce(:+)

but compared to this it seems a bit creaky:

    HEADERS =3D ENV["BLAH_BLAH"]           +?=20
              ENV["BLAH_BLAH_BLAH"]      +?
              ENV["BLAH_BLAH_BLAH_BLAH"]

Is there a way to define infix operators in Ruby, as I quite like this =
one? :)

Actually, looking at the code they'd be ENV["BLAH_BLAH"].split(":") as =
they were PATHs, but I'm not sure the inclusion adds anything to the =
examples.

Regards,
Iain=