[holy ruby programmers batman!] Potential addition: nil safe chaining
gilesb at gmail.com
Sun Dec 23 13:46:29 EST 2007
> > 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.
All generalizations are false.
> 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.
Looks cool. I'm not into adding it to the belt at the moment but I can
definitely see the usefulness in some situations.
More information about the Utilitybelt-tinkering