On 25 Mar 2011, at 14:32, Colin B. wrote:
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 = ENV["BLAH_BLAH"] +
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 = [
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 = [
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 = ENV["BLAH_BLAH"] +?
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