[holy ruby programmers batman!] Potential addition: nil safe chaining

Tim Connor timocratic at gmail.com
Sat Dec 22 03:29:19 EST 2007

On Dec 21, 2007 6:21 PM, Giles Bowkett <gilesb at gmail.com> wrote:
> I'm thinking the egregious Law of Demeter thing is probably valid -
> Utility Belt is for heroes not villains. Actually, though, I don't
> want to seem dumb, but what's the use case?

Yeah, I hear the complaints, just not sure I agree with the validity,
of the application of said Universal Principle. ;)  Myabe I am jus a
hack, though.

It's purely sugar, so nothing that isn't replaced via longer
conditionals. My first (and actual) use case was (where error_div
might be nil):

error_message = error_div.chain(:inner_html)

or if I wanted to expand it out further (semi-theoretically: pulled
out my ass and does not quite match actual code needs, at the time):

error_message = page_ref.chain(:find_error_div, :inner_html)

Yeah, this could be abstracted differently, but the general pattern of
"if object and object.certain_method" as a conditional does come up
fairly often, as well.

More information about the Utilitybelt-tinkering mailing list