One type of construct in ruby that somewhat annoys me and IMHO reduces
readability is working around cases where an intermediate method might
return nil. Those cases are e.g.
while (line = gets && line.chomp)
foo = (bar && bar.baz && bar.baz.quuz) || default # bar might be nil, so
might bar.baz and bar.baz.quuz
There are some more constructs, most of you surely already used such
stuff or just spread it onto a bit more of lines.
While all of this is fine, maybe that could be improved. My RCR would be
to introduce a new syntax besides . for method invocation, which on the
first nil aborts any further method calls on it and returns nil.
E.g. the above examples would become:
while line = gets->chomp
foo = bar->baz->quuz || default
-> was the first best that came to my mind, if you have better ideas,
please state.
Unlike e.g. making nil to respond to every method returning self, this
solution would put the coder in power as to where he cares about getting
nil (e.g. he wants an exception if foo is nil, so he just writes
foo.bar, if he doesn’t want one, he writes foo->bar) and where he just
wants to get the nil value back.
Do you think this RCR is worth to make?
Regards
Stefan