From: "ara.t.howard" Date: 2007-07-21T02:24:44+09:00 Subject: Re: RCR? Added syntax for chains that possibly return nil On Jul 19, 2007, at 7:31 AM, Stefan Rusterholz wrote: > 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. i feel your pain, however i feel that moves towards calling things and having the side effect ignored will lead to new classes of subtle bugs. for instance path.gsub! File::SEPARATOR, '\\' returns nil for no sub for a reason: i may be an error if no substitution was done... still, i agree there are cases where i makes sense to chain/ignore > > while (line = gets && line.chomp) while( (line = gets or break).chomp ) end but i'm just playing devil's advocate there ;-) > 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? it does, but in the meantime we can use something like: http://drawohara.tumblr.com/post/6304449 a @ http://drawohara.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama